From b5e3a450900411dc3e845f29132268efca1f6dc4 Mon Sep 17 00:00:00 2001 From: rhamill1 Date: Mon, 12 Sep 2016 18:09:54 -0700 Subject: [PATCH] adds my new functions --- index.js | 13 +++++++------ myEach.js | 4 +++- myMap.js | 6 +++++- myReduce.js | 21 +++++++++++++++++---- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 93cd82f..a1a228d 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ var myEach = require('./myEach'); -var myMap = require('./myEach'); -var myReduce = require('./myEach'); +var myMap = require('./myMap'); +var myReduce = require('./myReduce'); /* ********************************************************************* You can edit this file It will make use of your code in myEach.js, myMap.js and myReduce.js @@ -12,10 +12,11 @@ var numArray = [0,1,10,100,1000]; /* myEach */ -// -// myEach(numArray, function print(element, index, arr) { -// console.log('inside myEach', element, index, arr); -// }); +// function print(item) { +// console.log(item)} + + + diff --git a/myEach.js b/myEach.js index e95bc02..b712273 100644 --- a/myEach.js +++ b/myEach.js @@ -2,7 +2,9 @@ function myEach(arr, callback) { -// CODE INSIDE HERE // + + for (var i = 0; i < arr.length; i++) + callback(arr[i], i, arr); } diff --git a/myMap.js b/myMap.js index ccb09c3..edf41c0 100644 --- a/myMap.js +++ b/myMap.js @@ -2,7 +2,11 @@ function myMap(arr, callback) { -// CODE INSIDE HERE // + newArray = []; + + for (var i = 0; i < arr.length; i++) { + newArray.push(callback(arr[i], i, arr)); } + return newArray; } diff --git a/myReduce.js b/myReduce.js index 174fbe3..8ce8c98 100644 --- a/myReduce.js +++ b/myReduce.js @@ -1,10 +1,23 @@ // 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) { - -// CODE INSIDE HERE // - +function myReduce(arr, callback, init) { + + var now = arr[0]; + if (!init) { + for (var i = 1; i < arr.length; i++) { + now = callback(now, arr[i], i, arr); + } + } + + else { + var now = init; + for (var i = 0; i < arr.length; i++) { + now = callback(now, arr[i], i, arr); + } + } + + return now; } /*