From b8fc7be76d8a3076cac2b599ebb9ca1c49cf5b68 Mon Sep 17 00:00:00 2001 From: Alivia Blount Date: Mon, 12 Sep 2016 16:36:56 -0700 Subject: [PATCH] In class callback and iterator practice --- myEach.js | 4 +++- myMap.js | 6 +++++- myReduce.js | 21 +++++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/myEach.js b/myEach.js index e95bc02..13bd8fc 100644 --- a/myEach.js +++ b/myEach.js @@ -3,7 +3,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..fc60233 100644 --- a/myMap.js +++ b/myMap.js @@ -3,7 +3,11 @@ function myMap(arr, callback) { // CODE INSIDE HERE // - + newArray = []; + for(var i = 0; i < arr.length; i++){ + newArray.push(callback(i)) + } + return newArray } /* diff --git a/myReduce.js b/myReduce.js index 174fbe3..9b7e590 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, initialValue) { + + // CODE INSIDE HERE // + var previousValue = arr[0]; + var currentValue = arr[1]; + var currentIndex = 1 + var total = 0; + + + for(var i = 0; i < arr.length; i++){ + callback(previousValue, currentIndex, currentValue, arr) + total = previousValue + currentValue; + previousValue = arr[currentIndex + 1] + currentValue = arr[currentIndex + 2] + currentIndex += 1 + } + return total; } /*