From 9231c12c58ad3423f7f3cab51ad18c695df77890 Mon Sep 17 00:00:00 2001 From: Natalia Hess Date: Mon, 12 Sep 2016 17:01:34 -0700 Subject: [PATCH] creates myEach myMap myReduce functions --- index.js | 37 +++++++++++++++++++++++++------------ myEach.js | 8 ++++---- myMap.js | 8 +++++--- myReduce.js | 20 +++++++++++++++++--- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 93cd82f..841d411 100644 --- a/index.js +++ b/index.js @@ -7,27 +7,40 @@ var myReduce = require('./myEach'); To run it on the console do: `node index.js` ***********************************************************************/ -var numArray = [0,1,10,100,1000]; +/* myEach */ +var numArray = [0,1,10,100,1000]; -/* myEach */ +function myEach(array, callback) { + for (var i = 0; i < array.length; i++) { + callback(array[i], i, array); + } +} // -// myEach(numArray, function print(element, index, arr) { -// console.log('inside myEach', element, index, arr); -// }); +myEach(numArray, function print(element, index, arr) { + console.log('inside myEach', element, index, arr); +}); +/* myMap */ + +function myMap(arr, callback) { + var mappedArray = []; + for (var i = 0; i < arr.length; i++) { + mappedArray.push(callback(arr[i], i, arr)); + } + return mappedArray; +} -/* myMap */ -// var input = ["a","b","c"]; -// var output = myMap(input, function capitalize(v){ -// return v.toUpperCase(); -// }); -// console.log('Testing myMap') -// console.log(output === ["A", "B", "C"]) // assertion +var input = ["a","b","c"]; +var output = myMap(input, function capitalize(v){ + return v.toUpperCase(); +}); +console.log('Testing myMap') +console.log(output === ["A", "B", "C"]) // assertion console.log("the end"); diff --git a/myEach.js b/myEach.js index e95bc02..7e7bb5e 100644 --- a/myEach.js +++ b/myEach.js @@ -1,9 +1,9 @@ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ForEach -function myEach(arr, callback) { - -// CODE INSIDE HERE // - +function myEach(array, callback) { + for (var i = 0; i < array.length; i++) { + callback(array[i], i, array); + } } /* diff --git a/myMap.js b/myMap.js index ccb09c3..b4f5241 100644 --- a/myMap.js +++ b/myMap.js @@ -1,9 +1,11 @@ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Map function myMap(arr, callback) { - -// CODE INSIDE HERE // - + var mappedArray = []; + for (var i = 0; i < arr.length; i++) { + mappedArray.push(callback(arr[i], i, arr)); + } + return mappedArray; } /* diff --git a/myReduce.js b/myReduce.js index 174fbe3..d9000ee 100644 --- a/myReduce.js +++ b/myReduce.js @@ -1,11 +1,25 @@ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce for more details // Don't worry about initialValue at first. You can always add it in later. -function myReduce(arr, callback) { +function myReduce(arr, callback, initialValue) { + var i = 0; + var previousValue; + + if (initialValue === undefined) { + i++; + previousValue = arr[0]; + } else { + previousValue = initialValue; + } + + for (; i < arr.length; i++) { + previousValue = callback(previousValue, arr[i], i, arr); + } + return previousValue; +} + -// CODE INSIDE HERE // -} /* Best if you don't code out here.