From c9fdd4c8fbd8637a954d67d899a2be544f6913b0 Mon Sep 17 00:00:00 2001 From: David Finley Date: Thu, 22 Mar 2018 11:48:58 -0400 Subject: [PATCH 1/2] recursion and this complete --- src/recursion.js | 7 +++++++ src/this.js | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/recursion.js b/src/recursion.js index a6a6c13..772e4bf 100644 --- a/src/recursion.js +++ b/src/recursion.js @@ -3,11 +3,18 @@ const nFibonacci = (n) => { // fibonacci sequence: 1 1 2 3 5 8 13 ... // return the nth number in the sequence + if (n <= 2) return 1; + + return nFibonacci(n - 1) + nFibonacci(n - 2); }; const nFactorial = (n) => { // factorial example: !5 = 5 * 4 * 3 * 2 * 1 // return the factorial of `n` + if (n === 0) { + return 1; + } + return n * nFactorial(n - 1); }; /* Extra Credit */ diff --git a/src/this.js b/src/this.js index f0f994c..67fe32d 100644 --- a/src/this.js +++ b/src/this.js @@ -7,10 +7,15 @@ class User { constructor(options) { // set a username and password property on the user object that is created + this.username = options.username; + this.password = options.password; } // create a method on the User class called `checkPassword` // this method should take in a string and compare it to the object's password property // return `true` if they match, otherwise return `false` + checkPassword(pass) { + return this.password === pass; + } } const me = new User({ @@ -27,13 +32,17 @@ const checkPassword = function comparePasswords(passwordToCompare) { // use `this` to access the object's `password` property. // do not modify this function's parameters // note that we use the `function` keyword and not `=>` + return this.password === passwordToCompare; }; // invoke `checkPassword` on `me` by explicitly setting the `this` context // use .call, .apply, and .bind // .call +checkPassword.call(me, ['correcthorsebatterystaple']); // .apply +checkPassword.apply(me, ['correcthorsebatterystaple']); // .bind +checkPassword.bind(me, ['correcthorsebatterystaple']); From 737552dce1cd1225d85f5d9e12877960f3045e22 Mon Sep 17 00:00:00 2001 From: David Finley Date: Fri, 23 Mar 2018 01:32:02 -0400 Subject: [PATCH 2/2] prototype.js --- src/prototype.js | 127 +++++++++++++++++++++++++++++------------------ 1 file changed, 80 insertions(+), 47 deletions(-) diff --git a/src/prototype.js b/src/prototype.js index e2494a6..8bb9141 100644 --- a/src/prototype.js +++ b/src/prototype.js @@ -1,53 +1,86 @@ -/* - Object oriented design is commonly used in video games. For this part of the assignment - you will be implementing several classes with their correct inheritance heirarchy. - - In this file you will be creating three classes: - GameObject - createdAt - dimensions - destroy() // prototype method -> returns the string 'Game object was removed from the game.' - - NPC - hp - name - takeDamage() // prototype method -> returns the string ' took damage.' - // should inherit destroy() from GameObject's prototype - - Humanoid - faction - weapons - language - greet() // prototype method -> returns the string ' offers a greeting in .' +// Object oriented design is commonly used in video games. For this part of the assignment +// you will be implementing several classes with their correct inheritance heirarchy. + +// In this file you will be creating three classes: +// GameObject +// createdAt +// dimensions +// destroy() // prototype method -> returns the string 'Game object was removed from the game.' + + +class GameObject { + constructor(attributes) { + this.createdAt = attributes.createdAt; + this.dimensions = attributes.dimensions; + } + +GameObject.prototype.destroy = function() { + return `Game object was removed from the game.`; + // NPC + // hp + // name + // takeDamage() // prototype method -> returns the string ' took damage.' + // // should inherit destroy() from GameObject's prototype +function NPC(attributes) { + GameObject.call(this, attributes); + this.hp = attributes.hp; + this.name = attributes.name; +} + +NPC.prototype = Object.create(GameObject.prototype); + +NPC.prototype.takeDamage = function() { + return `${this.name} took damage.`; +} + + // Humanoid + // faction + // weapons + // language + // greet() // prototype method -> returns the string ' offers a greeting in .' // should inherit destroy() from GameObject through NPC // should inherit takeDamage() from NPC - Inheritance chain: Humanoid -> NPC -> GameObject - Instances of Humanoid should have all of the same properties as NPC and GameObject. - Instances of NPC should have all of the same properties as GameObject. - - Example: - - const hamsterHuey = new Humanoid({ - createdAt: new Date(), - dimensions: { - length: 2, - width: 1, - height: 1, - }, - hp: 5, - name: 'Hamster Huey', - faction: 'Gooey Kablooie', - weapons: [ - 'bubblegum', - ], - language: 'Hamsterish', - }); - - hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' - hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' - hamsterHuey.destroy(); // returns 'Game object was removed from the game.' -*/ +// Inheritance chain: Humanoid -> NPC -> GameObject +// Instances of Humanoid should have all of the same properties as NPC and GameObject. +// Instances of NPC should have all of the same properties as GameObject. + +function Humanoid(attributes) { + NPC.call(this, attributes); + this.faction = attributes.faction; + this.weapons = attributes.weapons; + this.language = attributes.language; +}; + +Humanoid.prototype = Object.create(NPC.prototype); + +thisHumanoid.prototype.greet() = function() { + return `${this.name} offers a greeting in ${this.language}.`; +}; + + +// Example: + +// const hamsterHuey = new Humanoid({ +// createdAt: new Date(), +// dimensions: { +// length: 2, +// width: 1, +// height: 1, +// }, +// hp: 5, +// name: 'Hamster Huey', +// faction: 'Gooey Kablooie', +// weapons: [ +// 'bubblegum', +// ], +// language: 'Hamsterish', +// }); + +// hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish' +// hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.' +// hamsterHuey.destroy(); // returns 'Game object was removed from the game.' +// */ /* eslint-disable no-undef */