diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..752b5f8d5 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -2,38 +2,57 @@ const assert = require('assert'); -function forEach(arr, callback) { - // Your code here -} - +const arr = [10,20,30]; function map(arr, callback) { - // Your code here -} + //create an empty array into which you will push the new elements + let newArray = []; + //loop through the original array + for (let i=0;i { - 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 +66,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; - }); - assert.deepEqual(filtered, [2]); - }); - }); - - 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; + describe('#reduce()', () => { + it('should return array elements added together', () => { + const reduced = reduce([1, 2, 3], (acc, num) => { + return acc + num; }); - assert.equal(somed, false); + assert.deepEqual(reduced, 6); }); }); - 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!') - }