Skip to content

Commit 65ccbf5

Browse files
committed
Prototypes, Recursion and This
1 parent de6985d commit 65ccbf5

3 files changed

Lines changed: 68 additions & 74 deletions

File tree

‎src/prototype.js‎

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,49 @@
1-
/*
2-
Object oriented design is commonly used in video games. For this part of the assignment
3-
you will be implementing several classes with their correct inheritance heirarchy.
4-
5-
In this file you will be creating three classes:
6-
GameObject
7-
createdAt
8-
dimensions
9-
destroy() // prototype method -> returns the string 'Game object was removed from the game.'
10-
11-
NPC
12-
hp
13-
name
14-
takeDamage() // prototype method -> returns the string '<object name> took damage.'
15-
// should inherit destroy() from GameObject's prototype
16-
17-
Humanoid
18-
faction
19-
weapons
20-
language
21-
greet() // prototype method -> returns the string '<object name> offers a greeting in <object language>.'
22-
// should inherit destroy() from GameObject through NPC
23-
// should inherit takeDamage() from NPC
24-
25-
Inheritance chain: Humanoid -> NPC -> GameObject
26-
Instances of Humanoid should have all of the same properties as NPC and GameObject.
27-
Instances of NPC should have all of the same properties as GameObject.
28-
29-
Example:
30-
31-
const hamsterHuey = new Humanoid({
32-
createdAt: new Date(),
33-
dimensions: {
34-
length: 2,
35-
width: 1,
36-
height: 1,
37-
},
38-
hp: 5,
39-
name: 'Hamster Huey',
40-
faction: 'Gooey Kablooie',
41-
weapons: [
42-
'bubblegum',
43-
],
44-
language: 'Hamsterish',
45-
});
1+
/* eslint-disable */
2+
3+
function GameObject(options) {
4+
this.createdAt = options.createdAt;
5+
this.dimensions = options.dimensions;
6+
}
7+
GameObject.prototype.destroy = function destroy() {
8+
return ('${this.name}was removed from the game');
9+
};
4610

47-
hamsterHuey.greet(); // returns 'Hamster Huey offers a greeting in Hamsterish'
48-
hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
49-
hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
50-
*/
11+
function NPC(npcOptions) {
12+
GameObject.call(this, npcOptions);
13+
this.hp = npcOptions.hp;
14+
this.name = npcOptions.name;
15+
};
16+
NPC.prototype = Object.create(GameObject.prototype);
17+
NPC.prototype.takedamage = function takeDamage() {
18+
return '${this.name}took damage.';
19+
};
5120

52-
/* eslint-disable no-undef */
21+
function Humanoid(humOptions) {
22+
NPC.call(this, humOptions);
23+
this.faction = humOptions.faction;
24+
this.weapons = humOptions.weapons;
25+
this.language = humOptions.language;
26+
}
27+
Humanoid.prototype.greet = function greet() {
28+
return '${this.name}offers a greeting in`${this.language}';
29+
}
30+
31+
const Bill = new Humanoid({
32+
createdAt: new Date(),
33+
dimensions: {
34+
length: 2,
35+
width: 1,
36+
height: 1,
37+
},
38+
hp: 5,
39+
name: 'Hamster Huey',
40+
faction: 'Gooey Kablooie',
41+
weapons: [
42+
'bubblegum',
43+
],
44+
language: 'Hamsterish',
45+
46+
});
5347

5448
module.exports = {
5549
GameObject,

‎src/recursion.js‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
// Complete the following functions.
22

33
const nFibonacci = (n) => {
4-
// fibonacci sequence: 1 2 3 5 8 13 ...
5-
// return the nth number in the sequence
4+
if (n <= 2) return 1;
5+
return nFibonacci(n - 1) + nFibonacci(n - 2);
66
};
77

8+
// fibonacci sequence: 1 2 3 5 8 13 ...
9+
// return the nth number in the sequence
10+
11+
812
const nFactorial = (n) => {
13+
if (n <= 1) return 1;
14+
return n * nFactorial(n - 1);
915
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1016
// return the factorial of `n`
1117
};
1218

13-
/* Extra Credit */
14-
const checkMatchingLeaves = (obj) => {
15-
// return true if every property on `obj` is the same
16-
// otherwise return false
17-
};
1819

1920
/* eslint-enable no-unused-vars */
2021

2122
module.exports = {
2223
nFibonacci,
2324
nFactorial,
24-
checkMatchingLeaves,
2525
};

‎src/this.js‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
// Follow the instructions and fill in the blank sections.
2-
// There are no tests for this file.
3-
// To verify your code works you can run this file using `node this.js` while in the `/src` folder
1+
class User {
42

5-
/* part 1 */
63

7-
class User {
84
constructor(options) {
95
// set a username and password property on the user object that is created
6+
this.username = options.username;
7+
this.password = options.password;
8+
}
9+
checkPassword(toCheck) {
10+
return this.password === toCheck;
1011
}
11-
// create a method on the User class called `checkPassword`
12-
// this method should take in a string and compare it to the object's password property
13-
// return `true` if they match, otherwise return `false`
1412
}
1513

1614
const me = new User({
1715
username: 'LambdaSchool',
1816
password: 'correcthorsebatterystaple',
1917
});
20-
18+
// create a method on the User class called `checkPassword`
19+
// this method should take in a string and compare it to the object's password property
20+
// return `true` if they match, otherwise return `false`
2121
const result = me.checkPassword('correcthorsebatterystaple'); // should return `true`
22-
23-
/* part 2 */
24-
2522
const checkPassword = function comparePasswords(passwordToCompare) {
23+
if (passwordToCompare === this.password) return true;
24+
return false;
2625
// recreate the `checkPassword` method that you made on the `User` class
2726
// use `this` to access the object's `password` property.
2827
// do not modify this function's parameters
2928
// note that we use the `function` keyword and not `=>`
3029
};
31-
30+
/* part 2 */
3231
// invoke `checkPassword` on `me` by explicitly setting the `this` context
3332
// use .call, .apply, and .bind
3433

3534
// .call
36-
35+
checkPassword.call(me, 'correcthorsebatterystaple');
3736
// .apply
38-
39-
// .bind
37+
checkPassword.apply(me, 'correcthorsebatterystaple');
38+
// .bind;
39+
checkPassword.bind(me, 'correcthorsebatterystaple');

0 commit comments

Comments
 (0)