Skip to content

Commit 1c51e81

Browse files
committed
finished JavaScript I (no stretches)
1 parent 3bfc4b1 commit 1c51e81

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/arrays.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,33 @@ const find = (elements, cb) => {
5353
// Look through each value in `elements` and pass each element to `cb`.
5454
// If `cb` returns `true` then return that element.
5555
// Return `undefined` if no elements pass the truth test.
56+
for (let i = 0; i < elements.length; i++) {
57+
if (cb(elements[i])) {
58+
return elements[i];
59+
}
60+
}
61+
return undefined;
5662
};
5763

5864
const filter = (elements, cb) => {
5965
// Do NOT use .filter, to complete this function.
6066
// Similar to `find` but you will return an array of all elements that passed the truth test
6167
// Return an empty array if no elements pass the truth test
68+
const newArr = [];
69+
for (let i = 0; i < elements.length; i++) {
70+
if (cb(elements[i])) {
71+
newArr.push(elements[i]);
72+
}
73+
}
74+
return newArr;
6275
};
6376

6477
/* STRETCH PROBLEM */
6578

6679
const flatten = (elements) => {
6780
// Flattens a nested array (the nesting can be to any depth).
6881
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
82+
return reduce(elements, (acc, el) => acc.concat(Array.isArray(el) ? flatten(el) : el), []);
6983
};
7084

7185
/* eslint-enable no-unused-vars, max-len */

src/closure.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,39 @@ const counter = () => {
55
// Example: const newCounter = counter();
66
// newCounter(); // 1
77
// newCounter(); // 2
8+
// const counter = () => {
9+
let ncounter = 0;
10+
return () => {
11+
ncounter++;
12+
return ncounter;
13+
};
814
};
915

1016
const counterFactory = () => {
1117
// Return an object that has two methods called `increment` and `decrement`.
1218
// `increment` should increment a counter variable in closure scope and return it.
1319
// `decrement` should decrement the counter variable and return it.
20+
let count = 0;
21+
const newMethod = {
22+
increment: () => {
23+
return ++count;
24+
},
25+
decrement: () => {
26+
return --count;
27+
}
28+
};
29+
return newMethod;
1430
};
1531

1632
const limitFunctionCallCount = (cb, n) => {
1733
// Should return a function that invokes `cb`.
1834
// The returned function should only allow `cb` to be invoked `n` times.
35+
let cbCount = 0;
36+
return (...arg) => {
37+
if (cbCount === n) return null;
38+
cbCount++;
39+
return cb(...arg);
40+
};
1941
};
2042

2143
/* STRETCH PROBLEM */

src/objects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const mapObject = (obj, cb) => {
3939
const pairs = (obj) => {
4040
// Convert an object into a list of [key, value] pairs.
4141
// http://underscorejs.org/#pairs
42-
const entpairs = Object.entries(obj);
42+
return Object.entries(obj);
4343
};
4444

4545
/* STRETCH PROBLEMS */

yarn-error.log

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Arguments:
2-
C:\Program Files\nodejs\node.exe C:\Users\Sakkey\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js test arrays
2+
C:\Program Files\nodejs\node.exe C:\Users\Sakkey\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js test objects
33

44
PATH:
55
C:\Users\Sakkey\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\Sakkey\bin;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0;C:\Program Files (x86)\Skype\Phone;C:\Program Files\Git\cmd;C:\Program Files\nodejs;C:\Users\Sakkey\AppData\Local\Microsoft\WindowsApps;C:\Users\Sakkey\AppData\Local\GitHubDesktop\bin;C:\Users\Sakkey\AppData\Roaming\npm;C:\Program Files\Git\usr\bin\vendor_perl;C:\Program Files\Git\usr\bin\core_perl
@@ -3587,7 +3587,7 @@ Trace:
35873587
Error: Command failed.
35883588
Exit code: 1
35893589
Command: C:\WINDOWS\system32\cmd.exe
3590-
Arguments: /d /s /c eslint tests/*.js && eslint src/*.js && jest --verbose arrays
3590+
Arguments: /d /s /c eslint tests/*.js && eslint src/*.js && jest --verbose objects
35913591
Directory: C:\Users\Sakkey\lambdaschool\javascript-i
35923592
Output:
35933593

0 commit comments

Comments
 (0)