diff --git a/06week/higherOrder.js b/06week/higherOrder.js deleted file mode 100644 index 73926e3dc..000000000 --- a/06week/higherOrder.js +++ /dev/null @@ -1,103 +0,0 @@ -'use strict'; - -const assert = require('assert'); - -function forEach(arr, callback) { - // Your code here -} - -function map(arr, callback) { - // Your code here -} - -function filter(arr, callback) { - // Your code here -} - -function some(arr, callback) { - // Your code here -} - -function every(arr, callback) { - // Your code here -} - -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) => { - return num * num; - }); - it('should return new array with mapped items', () => { - assert.deepEqual(mapped, [1, 4, 9]); - }); - it('should not affect the original array', () => { - assert.deepEqual(arr, [1, 2, 3]); - }) - }); - - 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; - }); - assert.equal(somed, false); - }); - }); - - describe('#every()', () => { - it('should return true if at all passes the predicate test', () => { - const everied = every([2, 4, 6], (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); - }); - }); - -} else { - - console.log('Only run the tests on this one!') - -} diff --git a/06week/higherOrder/higherOrder.js b/06week/higherOrder/higherOrder.js new file mode 100644 index 000000000..42fcd0a1e --- /dev/null +++ b/06week/higherOrder/higherOrder.js @@ -0,0 +1,55 @@ +'use strict'; + + +function forEach(arr, callback) { + for (let i = 0; i < arr.length; i++){ + callback(arr) + } +} + + +function map(arr, callback) { + let newArray = [] + for (let i = 0; i < arr.length; i++){ + let newNum = callback(arr[i]) + newArray.push(newNum) + + } + return newArray; +} + +function filter(arr, callback) { + let newArray = [] + for (let i = 0; i < arr.length; i++){ + if(callback(arr[i]) == true){ + newArray.push(arr[i]) + }; + } + return newArray +} + +function some(arr, callback) { + for (let i = 0; i < arr.length; i++){ + if(callback(arr[i]) == true){ + return true + } + } + return false +} + +function every(arr, callback) { + for (let i = 0; i < arr.length; i++){ + if(callback(arr[i]) == false){ + return false; + } + } + return true +} + +module.exports = { + forEach, + map, + filter, + some, + every +} \ No newline at end of file diff --git a/06week/higherOrder/package.json b/06week/higherOrder/package.json new file mode 100644 index 000000000..f0e45d0fe --- /dev/null +++ b/06week/higherOrder/package.json @@ -0,0 +1,9 @@ +{ + "scripts": { + "start": "node higherOrder", + "test": "mocha" + }, + "devDependencies": { + "mocha": "^5.2.0" + } +} diff --git a/06week/higherOrder/test.js b/06week/higherOrder/test.js new file mode 100644 index 000000000..79f4b4e12 --- /dev/null +++ b/06week/higherOrder/test.js @@ -0,0 +1,88 @@ +'use strict'; + +const assert = require('assert'); +const forEach = require("./higherOrder").forEach; +const map = require("./higherOrder").map; +const filter = require("./higherOrder").filter; +const some = require("./higherOrder").some; +const every = require("./higherOrder").every; + +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) => { + return num * num; + }); + it('should return new array with mapped items', () => { + assert.deepEqual(mapped, [1, 4, 9]); + }); + it('should not affect the original array', () => { + assert.deepEqual(arr, [1, 2, 3]); + }) + }); + + 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; + }); + assert.equal(somed, false); + }); + }); + + describe('#every()', () => { + it('should return true if at all passes the predicate test', () => { + const everied = every([2, 4, 6], (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); + }); + }); + + } else { + + console.log('Only run the tests on this one!') + + } \ No newline at end of file