diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..250010720 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -6,9 +6,33 @@ function forEach(arr, callback) { // Your code here } -function map(arr, callback) { +function map(array, callback) { + let newArray = []; + for(let i=0; i < array.length; i++){ + let elements = callback(array[i]); + newArray.push(elements); + } + return newArray; // Your code here } +// const checkObject = [{price: 10},{price: 20},{price: 30}]; +// const checkArray = [10, 20, 30]; +// function reduce(array, accumulator) { +// accumulator = accumulator || 0; +// for (let index = 0; index < array.length; index++) { +// if(typeof array[index] == 'number') { +// accumulator = accumulator + array[index]; +// } else if(typeof array[index] == 'object') { +// for(let i in array[index]){ +// accumulator = accumulator + array[index][i]; +// } +// } +// } +// return accumulator; +// } +// const sum = reduce(checkArray, 10); // Set accumulator to 10 +// console.log("ANSWER: ",sum); + function filter(arr, callback) { // Your code here @@ -23,17 +47,6 @@ function every(arr, callback) { } if (typeof describe === 'function') { - - describe('#forEach()', () => { - it('should call the callback the array.length number of times', () => { - let count = 0; - forEach([1, 2, 3], () => { - count++; - }); - assert.equal(count, 3); - }); - }); - describe('#map()', () => { const arr = [1, 2, 3]; const mapped = map(arr, (num) => { @@ -47,57 +60,23 @@ if (typeof describe === 'function') { }) }); - describe('#filter()', () => { - it('should return an array of items that pass the predicate test', () => { - const filtered = filter([1, 2, 3], (num) => { - return num % 2 === 0; + describe('#reduce()', () => { + it('should return array elements added together', () => { + const reduced = reduce([1, 2, 3], (acc, num) => { + return acc + num; }); - assert.deepEqual(filtered, [2]); + assert.deepEqual(reduced, 6); }); }); - describe('#some()', () => { - let count = 0; - const somed = some([1, 2, 3, 4], (num) => { - count++; - return num % 2 === 0; - }); - it('should return true if at least one item passes the predicate test', () => { - assert.equal(somed, true); - }); - it('should stop at the first item that passes the predicate test', () => { - assert.equal(count, 2); - }); - it('should return false if no items pass the predicate test', () => { - const somed = some([1, 3, 5], (num) => { - return num % 2 === 0; - }); - assert.equal(somed, false); - }); - }); - - describe('#every()', () => { - it('should return true if at all passes the predicate test', () => { - const everied = every([2, 4, 6], (num) => { + describe('#filter()', () => { + it('should return an array of items that pass the predicate test', () => { + const filtered = filter([1, 2, 3], (num) => { return num % 2 === 0; }); - assert.equal(everied, true); - }); - let count = 0; - const everied = every([2, 3, 4, 5], (num) => { - count++; - return num % 2 === 0; - }); - it('should return false if any item fails the predicate test', () => { - assert.equal(everied, false); - }); - it('should stop at the first item that fails the predicate test', () => { - assert.equal(count, 2); + assert.deepEqual(filtered, [2]); }); }); - } else { - console.log('Only run the tests on this one!') - }