From 999871a024024b233cd75fa50db6be31f28c639c Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 9 Nov 2017 17:37:13 -0600 Subject: [PATCH 1/4] Better understanding of first assignment --- 06week/higherOrder.js | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 06week/higherOrder.js diff --git a/06week/higherOrder.js b/06week/higherOrder.js new file mode 100644 index 000000000..e89f536c0 --- /dev/null +++ b/06week/higherOrder.js @@ -0,0 +1,71 @@ +'use strict'; + +const arr = ['', '', '', '', '', ''] + +// Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. +arr.forEach((item, index, arr) => { + +}); + +// Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. +arr.map((item, index, arr) => { + +}); + +// Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. +arr.filter((item, index, arr) => { + +}); + + + + + +// Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. + +const test = [1, 2, 3, 4, 5, 6]; + +const doThis = (arr, callbackFunc) => { + const newArr = []; + arr.forEach((i) => { + newArr.push(callbackFunc(i)); + }); + return newArr; +}; + +doThis(test, (num) => num + 1); + +const test2 = test.map((num, index) => { + return num + 1; +}); + +console.log(test2); + +console.log(test.map((num, index) => num + 1)); + +// Create an object and store it to a variable called userObject. It must have at least 8 key/value pairs. + +const userObject = { + name: 'Deyton', + middleName: 'A', + lastName: 'Koch', + age: 27, + eyeColor: 'Blue', + hairColor: 'Brown', + greeting: 'Hello!', + favoriteMovie: 'Wizard of Oz', +}; + +// 3. Create an array from all of the keys in userObject. Store the array in a variable called userKeyArray. + +const userKeyArray = Object.keys(userObject); +console.log(userKeyArray); + +// Create an array of all of the values in userObject using userKeyArray and your map() function, store it in a variable called userValueArray. + +// const userValueArray = Object.values(userObject); +// console.log(userValueArray); + +const userValueArray = []; + +doThis(userValleyArray, (userKeyArray) => userKeyArray.values(userObject)); From 462453157944438687c4d330bc5e45b1bad99252 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 9 Nov 2017 18:26:01 -0600 Subject: [PATCH 2/4] Attempt at forEachfunction --- 06week/higherOrder.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index e89f536c0..e4c7b1e25 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -1,16 +1,20 @@ 'use strict'; -const arr = ['', '', '', '', '', ''] - // Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. -arr.forEach((item, index, arr) => { -}); +let forEachArr = [1, 2, 3, 4, 5, 6]; + +const whatForEachDoes = (i, callbackFunc) => { + let newArr = []; + for (i = 0; i < forEachArr.length; i++) { + newArr.push(callbackFunc(i)); + } +}; + +whatForEachDoes(forEachArr, (num) => num + 1); // Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. -arr.map((item, index, arr) => { -}); // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. arr.filter((item, index, arr) => { @@ -21,11 +25,11 @@ arr.filter((item, index, arr) => { -// Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. +// 1. Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. const test = [1, 2, 3, 4, 5, 6]; -const doThis = (arr, callbackFunc) => { +const whatMapDoes = (arr, callbackFunc) => { const newArr = []; arr.forEach((i) => { newArr.push(callbackFunc(i)); @@ -33,8 +37,9 @@ const doThis = (arr, callbackFunc) => { return newArr; }; -doThis(test, (num) => num + 1); +whatMapDoes(test, (num) => num + 1); + // The map() method for reference: const test2 = test.map((num, index) => { return num + 1; }); @@ -43,7 +48,7 @@ console.log(test2); console.log(test.map((num, index) => num + 1)); -// Create an object and store it to a variable called userObject. It must have at least 8 key/value pairs. +// 2. Create an object and store it to a variable called userObject. It must have at least 8 key/value pairs. const userObject = { name: 'Deyton', @@ -68,4 +73,4 @@ console.log(userKeyArray); const userValueArray = []; -doThis(userValleyArray, (userKeyArray) => userKeyArray.values(userObject)); +whatMapDoes(userValleyArray, (userKeyArray) => userKeyArray.values(userObject)); From 7068521f39381845a14958885bb31ae2b2fc9414 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 9 Nov 2017 18:36:24 -0600 Subject: [PATCH 3/4] finish first attempt at project, but missing 5th inclass problem, never recieved --- 06week/higherOrder.js | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index e4c7b1e25..a19a8d202 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -1,13 +1,12 @@ 'use strict'; // Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. - +// let because for each does not return a new array let forEachArr = [1, 2, 3, 4, 5, 6]; -const whatForEachDoes = (i, callbackFunc) => { - let newArr = []; +const whatForEachDoes = (arr, callbackFunc) => { for (i = 0; i < forEachArr.length; i++) { - newArr.push(callbackFunc(i)); + forEachArr.push(callbackFunc(i)); } }; @@ -15,11 +14,30 @@ whatForEachDoes(forEachArr, (num) => num + 1); // Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. +const mapArr = [1, 2, 3, 4, 5, 6]; + +const whatMapDoes = (arr, callbackFunc) => { + let newArr = []; + mapArr.forEach((i) => { + newArr.push(callbackFunc(i)); + }); + return newArr; +}; + +whatMapDoes(mapArr, (num) => num + 1); // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. -arr.filter((item, index, arr) => { -}); +const filterArr = [1, 2, 3, 4, 5, 6]; + +const whatFilterDoes = (arr, callbackFunc) => { + let newArr = []; + filterArr.forEach((i) => { + newArr.push(callbackFunc(i)); + }); +}; + +whatFilterDoes(filterArr, (num) => num < 3); From 3b685d2651dd303d695357ff8d223de76f9e9db1 Mon Sep 17 00:00:00 2001 From: deytonk Date: Thu, 9 Nov 2017 19:35:26 -0600 Subject: [PATCH 4/4] class progress --- 06week/higherOrder.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index a19a8d202..ac4ab8234 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -39,7 +39,47 @@ const whatFilterDoes = (arr, callbackFunc) => { whatFilterDoes(filterArr, (num) => num < 3); +// For your project, we are going to go back into time and re-implement all of these higher-order functions from scratch. Place your code in the /06week/higherOrder.js file in your workbook. +// +// 1. Create a some() function that takes an array of items and a function that returns true or false if any of the items return true in the function. + +const mySome = (arr, callBack) => { + let somethingPasses = false; + const breakException = {}; + arr.forEach((item) => { + if (callBack(item)) { + somethingPasses = true; + throw breakException; + } + }); + + return somethingPasses; +}; + +const someArr = [5, 7, 10, 400, 10000, 5]; +const test = mySome(someArr, (item) => {return item > 50}); + +console.log(test); + +// 2. Create an every() function that takes an array of items and a function that returns true or false if all of the items return true in the function. + +const myEvery = (arr, callBack) => { + let somethingPasses = true; + const breakException = {}; + arr.forEach((item) => { + if (callBack(item)) { + somethingPasses = false; + throw breakException; + } + }); + + return somethingPasses; +}; + +const everyArr = [5, 7, 10, 400, 10000, 5]; +const test = myEvery(everyArr, (item) => {return item > 50}); +console.log(test);