Skip to content

Commit 45e1f4a

Browse files
committed
finished arrays and closure
1 parent ddd493f commit 45e1f4a

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

‎src/arrays.js‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,89 @@
33

44

55
const each = (elements, cb) => {
6+
// making a loop and running the function cb on all the elements
7+
for (let i = 0; i < elements.length; i++) {
8+
cb(elements[i], i);
9+
}
610
// Iterates over a list of elements, yielding each in turn to the `cb` function.
711
// This only needs to work with arrays.
812
// based off http://underscorejs.org/#each
913
};
1014

1115
const map = (elements, cb) => {
16+
// make the new array to be mapped on
17+
const newarray = [];
18+
// putting in the values for the map
19+
each(elements, (ans) => {
20+
newarray.push(cb(ans));
21+
});
22+
// for (let i = 0; i < elements.length; i++) {
23+
// newarray.push(cb(elements[i]));
24+
// }
25+
return newarray;
1226
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
1327
// Return the new array.
1428
};
1529

1630
const reduce = (elements, cb, memo) => {
31+
// defining total
32+
let total = 0;
33+
// looping through the array
34+
for (let i = 0; i < elements.length; i++) {
35+
// checking for a memo
36+
if (i === 0) {
37+
if (memo) total = memo;
38+
// changing the type of total if its a string
39+
else if (typeof (elements[i]) === 'string') total = '';
40+
}
41+
total = cb(total, elements[i]);
42+
}
43+
return total;
44+
// fix this
1745
// Combine all elements into a single value going from left to right.
1846
// Elements will be passed one by one into `cb`.
1947
// `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value.
2048
};
2149

2250
const find = (elements, cb) => {
51+
let counter = 0;
52+
for (let i = 0; i < elements.length; i++) {
53+
if (cb(elements[i]) === true) {
54+
counter += 1;
55+
return elements[i];
56+
}
57+
}
58+
if (counter === 0) return 'undefined';
2359
// Look through each value in `elements` and pass each element to `cb`.
2460
// If `cb` returns `true` then return that element.
2561
// Return `undefined` if no elements pass the truth test.
2662
};
2763

2864
const filter = (elements, cb) => {
65+
const arr = [];
66+
let counter = 0;
67+
for (let i = 0; i < elements.length; i++) {
68+
if (cb(elements[i]) === true) {
69+
arr[counter] = elements[i];
70+
counter += 1;
71+
}
72+
}
73+
return arr;
2974
// Similar to `find` but you will return an array of all elements that passed the truth test
3075
// Return an empty array if no elements pass the truth test
3176
};
3277

3378
const flatten = (elements) => {
79+
let flatarr = [];
80+
each(elements, (elem) => {
81+
if (Array.isArray(elem)) {
82+
const nestedarray = flatten(elem);
83+
flatarr = flatarr.concat(nestedarray);
84+
} else {
85+
flatarr.push(elem);
86+
}
87+
});
88+
return flatarr;
3489
// Flattens a nested array (the nesting can be to any depth).
3590
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
3691
};

‎src/closure.js‎

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

33
const counter = () => {
4+
let result = 0;
5+
const fx = () => {
6+
return result += 1;
7+
};
8+
return fx;
49
// Return a function that when invoked increments and returns a counter variable.
510
// Example: const newCounter = counter();
611
// newCounter(); // 1
712
// newCounter(); // 2
813
};
914

1015
const counterFactory = () => {
16+
let number = 0;
17+
const obj = {
18+
increment() {
19+
return number += 1;
20+
},
21+
decrement() {
22+
return number -= 1;
23+
}
24+
};
25+
return obj;
26+
1127
// Return an object that has two methods called `increment` and `decrement`.
1228
// `increment` should increment a counter variable in closure scope and return it.
1329
// `decrement` should decrement the counter variable and return it.
1430
};
1531

1632
const limitFunctionCallCount = (cb, n) => {
33+
let limit = 0;
34+
const fx = (...args) => {
35+
if (limit < n) {
36+
limit += 1;
37+
return cb(...args);
38+
}
39+
return null;
40+
};
41+
return fx;
1742
// Should return a function that invokes `cb`.
1843
// The returned function should only allow `cb` to be invoked `n` times.
1944
};
2045

2146
const cacheFunction = (cb) => {
47+
const cache = {};
48+
const fx = (args) => {
49+
if (!Object.prototype.hasOwnProperty.apply(cache, [args])) {
50+
cache[args] = cb(args);
51+
return cache[args];
52+
}
53+
return cache[args];
54+
};
55+
return fx;
2256
// Should return a funciton that invokes `cb`.
2357
// A cache (object) should be kept in closure scope.
2458
// The cache should keep track of all arguments have been used to invoke this function.

0 commit comments

Comments
 (0)