Skip to content
This repository was archived by the owner on May 30, 2019. It is now read-only.

Commit 7a9e01f

Browse files
committed
Refactored some code, cleaned up the reduce and others
1 parent 448daa3 commit 7a9e01f

File tree

5 files changed

+66
-46
lines changed

5 files changed

+66
-46
lines changed

src/arrays.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,20 @@ const reduce = (elements, cb, memo) => {
1616
// Elements will be passed one by one into `cb`.
1717
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
1818

19-
// TODO come back to later
20-
if (memo) memo = elements.shift();
19+
if (!memo) memo = elements.shift();
2120
elements.forEach((value) => {
22-
return cb(memo + value);
21+
memo = cb(memo, value); // Running total.
2322
});
23+
return memo;
2424
};
2525

2626
const find = (elements, cb) => {
2727
// Look through each value in `elements` and pass each element to `cb`.
2828
// If `cb` returns `true` then return that element.
2929
// Return `undefined` if no elements pass the truth test.
30-
31-
// TODO come back to later.
32-
elements.forEach((value, i) => {
33-
if (cb(value)) {
34-
return value;
35-
}
36-
});
30+
for (let i = 0; i < elements.length; i++) {
31+
if (cb(elements[i])) return elements[i];
32+
}
3733
return 'undefined';
3834
};
3935

@@ -52,9 +48,6 @@ const filter = (elements, cb) => {
5248
const flatten = (elements) => {
5349
// Flattens a nested array (the nesting can be to any depth).
5450
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
55-
56-
// For each value in array,
57-
// If value is an array, recurse into the value array.
5851
const arr = [];
5952
elements.forEach((value) => {
6053
// First make base case.

src/class.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ class User {
1010
this.email = options.email;
1111
this.password = options.password;
1212
}
13-
comparePasswords(pwStr) {
14-
if (pwStr === this.password) return true;
15-
return false;
16-
}
1713
}
1814

15+
User.prototype.comparePasswords = function (str) {
16+
if (str === this.password) return true;
17+
return false;
18+
};
19+
1920
/* eslint-disable no-undef */ // Remove this comment once you write your classes.
2021

2122

@@ -30,19 +31,19 @@ class User {
3031
class Animal {
3132
constructor(options) {
3233
this.age = options.age;
33-
}
34-
growOlder() {
35-
return this.age += 1;
34+
this.growOlder = function () {
35+
return this.age += 1;
36+
};
3637
}
3738
}
3839

3940
class Cat extends Animal {
4041
constructor(options) {
4142
super(options);
4243
this.name = options.name;
43-
}
44-
meow() {
45-
return `${this.name} meowed!`;
44+
this.meow = function () {
45+
return `${this.name} meowed!`;
46+
};
4647
}
4748
}
4849

src/closure.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const counter = () => {
66
// newCounter(); // 1
77
// newCounter(); // 2
88
let count = 0;
9-
return () => {
9+
return function () {
1010
return count += 1;
1111
};
1212
};
@@ -16,21 +16,23 @@ const counterFactory = () => {
1616
// `increment` should increment a counter variable in closure scope and return it.
1717
// `decrement` should decrement the counter variable and return it.
1818
let count = 0;
19-
return { // Return an object that contains two methods
20-
increment: () => (count += 1),
21-
decrement: () => (count -= 1)
19+
const obj = {};
20+
obj.increment = () => {
21+
return count += 1;
22+
};
23+
obj.decrement = () => {
24+
return count -= 1;
2225
};
26+
return obj;
2327
};
2428

2529
const limitFunctionCallCount = (cb, n) => {
2630
// Should return a function that invokes `cb`.
2731
// The returned function should only allow `cb` to be invoked `n` times.
28-
let count = 0;
29-
return (...args) => { // Return a function, accept all parameters as one 'args' array.
30-
if (count !== n) {
31-
count++;
32-
return cb(...args); // In the cb parameters, expand the 'args' spread operator again. and pass them to returned cb.
33-
}
32+
let callCount = 0;
33+
return (...args) => {
34+
callCount++;
35+
if (n >= callCount) return cb(...args);
3436
return null;
3537
};
3638
};
@@ -42,16 +44,9 @@ const cacheFunction = (cb) => {
4244
// If the returned function is invoked with arguments that it has already seen
4345
// then it should return the cached result and not invoke `cb` again.
4446
// `cb` should only ever be invoked once for a given set of arguments.
45-
// const cache = {};
46-
// return function (arg) {
47-
// if (Object.keys(cache).includes(arg)) return cache[arg];
48-
// cache[arg] = cb(arg);
49-
// return cache[arg];
50-
// };
51-
52-
const cache = {}; // A cache object should be kept in closure scope to keep track of all arguments that have been used to invoke the function.
47+
const cache = {};
5348
return (input) => {
54-
if (Object.prototype.hasOwnProperty.call(cache, input)) return cache[input];
49+
if (Object.prototype.hasOwnProperty.apply(cache, [input])) return cache[input];
5550
cache[input] = cb(input);
5651
return cache[input];
5752
};

src/recursion.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,35 @@
33
const nFibonacci = (n) => {
44
// fibonacci sequence: 1 2 3 5 8 13 ...
55
// return the nth number in the sequence
6+
if (n <= 1) return 1;
7+
return nFibonacci(n - 2) + nFibonacci(n - 1);
68
};
79

810
const nFactorial = (n) => {
911
// factorial example: !5 = 5 * 4 * 3 * 2 * 1
1012
// return the factorial of `n`
13+
if (n <= 1) return 1;
14+
return n * nFactorial(n - 1);
1115
};
1216

1317
const checkMatchingLeaves = (obj) => {
14-
// return true if every property on `obj` is the same
15-
// otherwise return false
18+
// return true if every property on `obj` is the same.
19+
// otherwise return false.
20+
const obs = [obj]; // Set 'obs' to equal a 2d array of the object.
21+
let check;
22+
let flag = true;
23+
const helper = (o) => {
24+
const vals = Object.values(o);
25+
for (let i = 0; i < vals.length; i++) { // Loop through values in object to check if they're all the same.
26+
if (typeof vals[i] !== 'object' && !check) check = vals[i]; // Set check equal to the first value in the object.
27+
if (typeof vals[i] !== 'object' && check !== vals[i]) return flag = false; // If the current value is not an object AND check is not the same as the current value, return 'flag' as false.
28+
if (typeof vals[i] === 'object') obs.push(vals[i]); // If the current value IS an object, add the current value to the 'obs' object.
29+
}
30+
};
31+
for (let i = 0; i < obs.length; i++) { // Loop through the 'obs' array.
32+
helper(obs[i]); // For each value in the 'obs' array pass it to the helper function.
33+
}
34+
return flag; // Return the state of flag, which will only be false if specifically set false.
1635
};
1736

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

src/this.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
class User {
66
constructor(options) {
77
// set a username and password property on the user object that is created
8+
this.username = options.username;
9+
this.password = options.password;
10+
this.checkPassword = function (str) {
11+
if (str === this.password) return true;
12+
return false;
13+
};
814
}
915
// create a method on the User class called `checkPassword`
1016
// this method should take in a string and compare it to the object's password property
@@ -19,13 +25,19 @@ const checkPassword = function comparePasswords(passwordToCompare) {
1925
// use `this` to access the object's `password` property.
2026
// do not modify this function's parameters
2127
// note that we use the `function` keyword and not `=>`
28+
if (this.password === passwordToCompare) return true;
29+
return false;
2230
};
2331

2432
// invoke `checkPassword` on `me` by explicitly setting the `this` context
2533
// use .call, .apply, and .bind
2634

2735
// .call
28-
36+
checkPassword.call(me, 'correcthorsebatterystaple');
37+
checkPassword.call(me, 'incorrecthorsebatterystaple');
2938
// .apply
30-
39+
checkPassword.apply(me, ['correcthorsebatterystaple']);
40+
checkPassword.apply(me, ['incorrecthorsebatterystaple']);
3141
// .bind
42+
checkPassword.bind(me, 'correcthorsebatterystaple')();
43+
checkPassword.bind(me, 'incorrecthorsebatterystaple')();

0 commit comments

Comments
 (0)