Skip to content

Commit 627bf18

Browse files
committed
updated JavaScript-II hw
1 parent b3cabcd commit 627bf18

2 files changed

Lines changed: 25 additions & 14 deletions

File tree

src/recursion.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,27 @@ const nFactorial = (n) => {
2424
const checkMatchingLeaves = (obj) => {
2525
// return true if every property on `obj` is the same
2626
// otherwise return false
27+
let val;
28+
let allMatch = true;
29+
const checkLeaves = (object) => {
30+
Object.keys(object).forEach((key) => {
31+
if (val === undefined && typeof key !== 'object') {
32+
val = object[key];
33+
return;
34+
}
2735

28-
let objProperty = false;
29-
for (let i = 0; i < obj.length; i++) {
30-
if (Array.isArray(obj[i])) {
31-
checkMatchingLeaves(obj[i]);
32-
objProperty = true;
33-
}
34-
}
35-
return objProperty;
36+
if (typeof object[key] === 'object') {
37+
return checkLeaves(object[key]);
38+
}
39+
if (object[key !== val]) {
40+
allMatch = false;
41+
return;
42+
}
43+
return;
44+
});
45+
};
46+
checkLeaves(obj);
47+
return allMatch;
3648
};
3749

3850
/* eslint-enable no-unused-vars */

src/this.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,17 @@ const checkPassword = function comparePasswords(passwordToCompare) {
3636
// do not modify this function's parameters
3737
// note that we use the `function` keyword and not `=>`
3838

39-
if (me.password === passwordToCompare) {
40-
return true;
41-
}
42-
return false;
39+
return this.password === passwordToCompare;
4340
};
4441

4542
// invoke `checkPassword` on `me` by explicitly setting the `this` context
4643
// use .call, .apply, and .bind
44+
const correctPassword = 'correcthorsebatterystaple';
4745

4846
// .call
49-
checkPassword.call(me);
47+
checkPassword.call(me, correctPassword);
5048
// .apply
51-
checkPassword.apply(me);
49+
checkPassword.apply(me, [correctPassword]);
5250
// .bind
5351
const boundPassword = checkPassword.bind(me);
52+
boundPassword([correctPassword]);

0 commit comments

Comments
 (0)