File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 88// Return true if the potential password matches the `password` property. Otherwise return false.
99
1010// code here
11+ class User {
12+ constructor ( options ) {
13+ this . email = options . email ;
14+ this . password = options . password ;
15+ }
16+ comparePasswords ( potentialPassword ) {
17+ return this . password === potentialPassword ;
18+ }
19+ }
1120
1221// Part 2
1322// Create a class called `Animal` and a class called `Cat` using ES6 classes.
2029// property set on the Cat instance.
2130
2231// code here
32+ class Animal {
33+ constructor ( options ) {
34+ this . age = options . age ;
35+ }
36+ growOlder ( ) {
37+ return ++ this . age ;
38+ }
39+ }
40+
41+ class Cat extends Animal {
42+ constructor ( options ) {
43+ super ( options ) ;
44+ this . name = options . name ;
45+ }
46+ meow ( ) {
47+ return `${ this . name } meowed!` ;
48+ }
49+ }
2350
2451/* eslint-disable no-undef */
2552
Original file line number Diff line number Diff line change 1+
12/*
23 Object oriented design is commonly used in video games. For this part of the assignment
34 you will be implementing several classes with their correct inheritance heirarchy.
4849 hamsterHuey.takeDamage(); // returns 'Hamster Huey took damage.'
4950 hamsterHuey.destroy(); // returns 'Game object was removed from the game.'
5051*/
52+ class GameObject {
53+ constructor ( objAttr ) {
54+ this . createdAt = objAttr . createdAt ;
55+ this . dimensions = objAttr . dimensions ;
56+ }
57+ destroy ( ) {
58+ const name = this . name ;
59+ return 'Game object was removed from the game.' ;
60+ }
61+ }
62+ class NPC extends GameObject {
63+ constructor ( npcAttr ) {
64+ super ( npcAttr ) ;
65+ this . hp = npcAttr . hp ;
66+ this . name = npcAttr . name ;
67+ }
68+ takeDamage ( ) {
69+ return `${ this . name } took damage.` ;
70+ }
71+ }
72+
73+ class Humanoid extends NPC {
74+ constructor ( humAttr ) {
75+ super ( humAttr ) ;
76+ this . faction = humAttr . faction ;
77+ this . weapons = humAttr . weapons ;
78+ this . language = humAttr . language ;
79+ }
80+ greet ( ) {
81+ return `${ this . name } offers a greeting in ${ this . language } .` ;
82+ }
83+ }
5184
5285/* eslint-disable no-undef */
5386
Original file line number Diff line number Diff line change @@ -4,9 +4,9 @@ const nFibonacci = (n) => {
44 // fibonacci sequence: 1 1 2 3 5 8 13 ...
55 // return the nth number in the sequence
66 if ( n <= 1 ) {
7- return 1 ;
7+ return n ;
88 }
9- return nFibonacci ( n - 1 ) + nFibonacci ( n - 2 ) ;
9+ return nFibonacci ( n - 2 ) + nFibonacci ( n - 1 ) ;
1010} ;
1111
1212const nFactorial = ( n ) => {
@@ -22,6 +22,19 @@ const nFactorial = (n) => {
2222const checkMatchingLeaves = ( obj ) => {
2323 // return true if every property on `obj` is the same
2424 // otherwise return false
25+ // make a list of the values in obj
26+ const values = [ ] ;
27+ const getValues = ( object ) => {
28+ Object . values ( object ) . forEach ( ( value ) => {
29+ if ( Object ( value ) === value ) {
30+ getValues ( value ) ;
31+ } else {
32+ values . push ( value ) ;
33+ }
34+ } ) ;
35+ } ;
36+ getValues ( obj ) ;
37+ return values . every ( x => x === values [ 0 ] ) ;
2538} ;
2639
2740/* eslint-enable no-unused-vars */
Original file line number Diff line number Diff line change @@ -25,8 +25,6 @@ const me = new User({
2525
2626const result = me . checkPassword ( 'correcthorsebatterystaple' ) ; // should return `true`
2727
28- console . log ( result ) ;
29-
3028/* part 2 */
3129
3230const checkPassword = function comparePasswords ( passwordToCompare ) {
@@ -42,17 +40,15 @@ const checkPassword = function comparePasswords(passwordToCompare) {
4240
4341// .call
4442
45- console . log ( checkPassword . call ( me , 'correcthorsebatterystaple' ) ) ;
46- console . log ( checkPassword . call ( me , 'wrongpassword' ) ) ;
43+ checkPassword . call ( me , 'correcthorsebatterystaple' ) ;
44+ checkPassword . call ( me , 'wrongpassword' ) ;
4745
4846// .apply
4947
50- console . log ( checkPassword . apply ( me , [ 'correcthorsebatterystaple' ] ) ) ;
51- console . log ( checkPassword . apply ( me , [ 'wrongpassword' ] ) ) ;
48+ checkPassword . apply ( me , [ 'correcthorsebatterystaple' ] ) ;
49+ checkPassword . apply ( me , [ 'wrongpassword' ] ) ;
5250
5351// .bind
5452
5553const isTrue = checkPassword . bind ( me , 'correcthorsebatterystaple' ) ;
5654const isFalse = checkPassword . bind ( me , 'wrongpassword' ) ;
57- console . log ( isTrue ( ) ) ;
58- console . log ( isFalse ( ) ) ;
You can’t perform that action at this time.
0 commit comments