From 71a6254c6cf34547254647d3cc0a73fa68bd08f4 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Tue, 13 Nov 2018 20:13:04 -0600 Subject: [PATCH 1/6] map function complete --- 06week/higherOrder.js | 74 +++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..3fac62909 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -1,6 +1,6 @@ -'use strict'; +"use strict"; -const assert = require('assert'); +const assert = require("assert"); function forEach(arr, callback) { // Your code here @@ -8,6 +8,24 @@ function forEach(arr, callback) { function map(arr, callback) { // Your code here + const myMap = (arr, callback) => { + const newArr = []; + for (let i = 0; i < arr.length; i++) { + console.log(arr[i]); + const reformattedItem = callback(arr[i]); + newArr.push(reformattedItem); + } + return newArr; + }; + + const nameArr = ["hanna", "daniel", "abby", "lauren", "bob", "susan"]; + + const nameArrUpperCase = () => + myMap(nameArr, str => { + return str.toUpperCase(); + }); + + nameArrUpperCase(); } function filter(arr, callback) { @@ -22,10 +40,9 @@ function every(arr, callback) { // Your code here } -if (typeof describe === 'function') { - - describe('#forEach()', () => { - it('should call the callback the array.length number of times', () => { +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++; @@ -34,70 +51,67 @@ if (typeof describe === 'function') { }); }); - describe('#map()', () => { + describe("#map()", () => { const arr = [1, 2, 3]; - const mapped = map(arr, (num) => { + const mapped = map(arr, num => { return num * num; }); - it('should return new array with mapped items', () => { + it("should return new array with mapped items", () => { assert.deepEqual(mapped, [1, 4, 9]); }); - it('should not affect the original array', () => { + 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) => { + 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()', () => { + describe("#some()", () => { let count = 0; - const somed = some([1, 2, 3, 4], (num) => { + 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', () => { + 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', () => { + 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) => { + 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("#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) => { + const everied = every([2, 3, 4, 5], num => { count++; return num % 2 === 0; }); - it('should return false if any item fails the predicate test', () => { + 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', () => { + 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!') - + console.log("Only run the tests on this one!"); } From fd2f4ba2f31798d7d826458cd32aa3ef8a9ad659 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Sat, 17 Nov 2018 15:11:59 -0600 Subject: [PATCH 2/6] starting point --- 06week/higherOrder.js | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 3fac62909..ba586992e 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -4,39 +4,32 @@ const assert = require("assert"); function forEach(arr, callback) { // Your code here + for (let count = 0; count >= arr.length; count++) { + return callback(); + } } function map(arr, callback) { // Your code here - const myMap = (arr, callback) => { - const newArr = []; - for (let i = 0; i < arr.length; i++) { - console.log(arr[i]); - const reformattedItem = callback(arr[i]); - newArr.push(reformattedItem); - } - return newArr; - }; - - const nameArr = ["hanna", "daniel", "abby", "lauren", "bob", "susan"]; - - const nameArrUpperCase = () => - myMap(nameArr, str => { - return str.toUpperCase(); - }); - - nameArrUpperCase(); + arr.forEach(){ + return arr[i]*i; + } } function filter(arr, callback) { // Your code here + if(filtered[i] % 2 === 0){ + return i; + } } function some(arr, callback) { + //can use forEach // Your code here } function every(arr, callback) { + //can use forEach // Your code here } From 2409de6109261df970b77143f574abfcd21c23e6 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 19 Nov 2018 19:56:03 -0600 Subject: [PATCH 3/6] all tests passing --- 06week/higherOrder.js | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index ba586992e..e2abaefb4 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -4,33 +4,49 @@ const assert = require("assert"); function forEach(arr, callback) { // Your code here - for (let count = 0; count >= arr.length; count++) { - return callback(); + for (let count = 0; count < arr.length; count++) { + callback(arr[count]); } } function map(arr, callback) { // Your code here - arr.forEach(){ - return arr[i]*i; - } + const newArr = []; + arr.forEach(item => { + newArr.push(callback(item)); + }); + return newArr; } function filter(arr, callback) { // Your code here - if(filtered[i] % 2 === 0){ - return i; - } + const filteredArr = []; + arr.forEach(item => { + if (callback(item)) { + filteredArr.push(item); + } + }); + return filteredArr; } function some(arr, callback) { - //can use forEach // Your code here + for (let i = 0; i < arr.length; i++) { + if (callback(arr[i])) { + return true; + } + } + return false; } function every(arr, callback) { - //can use forEach // Your code here + for (let i = 0; i < arr.length; i++) { + if (!callback(arr[i])) { + return false; + } + } + return true; } if (typeof describe === "function") { From cd2f925e0b51e8d3796469766b2e9592a270d728 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 19 Nov 2018 20:03:27 -0600 Subject: [PATCH 4/6] comments added --- 06week/higherOrder.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index e2abaefb4..fc2b97953 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -12,7 +12,9 @@ function forEach(arr, callback) { function map(arr, callback) { // Your code here const newArr = []; + // for each item in the array, call the callback function on it arr.forEach(item => { + //then mutate the item and push it to new array newArr.push(callback(item)); }); return newArr; @@ -21,6 +23,7 @@ function map(arr, callback) { function filter(arr, callback) { // Your code here const filteredArr = []; + //for each item in the array, run the callback function and if it passes the test push original item to the filteredArr arr.forEach(item => { if (callback(item)) { filteredArr.push(item); @@ -31,6 +34,7 @@ function filter(arr, callback) { function some(arr, callback) { // Your code here + // Go through each item in the array until you find an item that passes the callback function for (let i = 0; i < arr.length; i++) { if (callback(arr[i])) { return true; @@ -41,6 +45,7 @@ function some(arr, callback) { function every(arr, callback) { // Your code here + // Go through each item in the array until you find an item that does not pass the callback function for (let i = 0; i < arr.length; i++) { if (!callback(arr[i])) { return false; From 78aa203bcc2f2b942dcc2c01abb1eef8993da2d8 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Mon, 19 Nov 2018 20:08:33 -0600 Subject: [PATCH 5/6] committing in order to reopen --- 06week/functional-javascript/hello-world.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 06week/functional-javascript/hello-world.js diff --git a/06week/functional-javascript/hello-world.js b/06week/functional-javascript/hello-world.js new file mode 100644 index 000000000..0493038e7 --- /dev/null +++ b/06week/functional-javascript/hello-world.js @@ -0,0 +1,5 @@ +const input = "hello world"; +const upperCaser = input => { + return input.toUpperCase(); +}; +console.log(upperCaser(input)); \ No newline at end of file From c82a9737831fbfcc06a78006c87091111265f7c4 Mon Sep 17 00:00:00 2001 From: Abby Gottlich Date: Wed, 5 Dec 2018 19:46:49 -0600 Subject: [PATCH 6/6] refactored filter and map functions to no longer use methods; all 10 tests passing --- 06week/higherOrder.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index fc2b97953..07c2d6179 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -12,11 +12,12 @@ function forEach(arr, callback) { function map(arr, callback) { // Your code here const newArr = []; - // for each item in the array, call the callback function on it - arr.forEach(item => { - //then mutate the item and push it to new array - newArr.push(callback(item)); - }); + for (let item = 0; item < arr.length; item++) { + // for each item in the array, call the callback function on it + const mutatedItem = callback(arr[item]); + // push the mutated items into the new array + newArr.push(mutatedItem); + } return newArr; } @@ -24,11 +25,12 @@ function filter(arr, callback) { // Your code here const filteredArr = []; //for each item in the array, run the callback function and if it passes the test push original item to the filteredArr - arr.forEach(item => { + for (let index = 0; index < arr.length; index++) { + const item = arr[index]; if (callback(item)) { filteredArr.push(item); } - }); + } return filteredArr; }