From 369e0c5ef4227369425470e8fd2fd51b64d1e837 Mon Sep 17 00:00:00 2001 From: sherri010 Date: Mon, 12 Sep 2016 16:35:20 -0700 Subject: [PATCH 1/4] adding myEach and myMap --- index.js | 36 ++++++++++++++++++++++++++++-------- myEach.js | 8 +++++--- myMap.js | 9 ++++++--- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 93cd82f..9f80096 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 @@ -17,16 +17,36 @@ var numArray = [0,1,10,100,1000]; // console.log('inside myEach', element, index, arr); // }); +// function myEach(arr,callback){ +// for(var i=0;i Date: Mon, 12 Sep 2016 16:39:21 -0700 Subject: [PATCH 2/4] adding myEach and myMap --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 93cd82f..0f7cf47 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ var myReduce = require('./myEach'); To run it on the console do: `node index.js` ***********************************************************************/ -var numArray = [0,1,10,100,1000]; +//var numArray = [0,1,10,100,1000]; /* myEach */ @@ -30,4 +30,4 @@ var numArray = [0,1,10,100,1000]; // console.log(output === ["A", "B", "C"]) // assertion -console.log("the end"); +//console.log("the end"); From 0b4b807a2297c1c844e16c2c0e8919766b0055d7 Mon Sep 17 00:00:00 2001 From: sherri010 Date: Mon, 12 Sep 2016 17:03:38 -0700 Subject: [PATCH 3/4] adding myReduce --- myReduce.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/myReduce.js b/myReduce.js index 174fbe3..93d2fe5 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) { +function myReduce(arr, callback,initialValue) { + var result; -// CODE INSIDE HERE // + if(initialValue != undefined){ + result=initialValue; + } + else { + result=arr[0]; + } + if(arr.length > 0) { + +for(var i=1 ;i < arr.length ;i++){ + result= callback(result, arr[i],i,arr); +} +} +return result; } /* From dec4e4e006a6091e5a8f95ba2ea9ac65b5c91f52 Mon Sep 17 00:00:00 2001 From: sherri010 Date: Mon, 12 Sep 2016 18:04:09 -0700 Subject: [PATCH 4/4] finished reduce --- myReduce.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/myReduce.js b/myReduce.js index 93d2fe5..18a230c 100644 --- a/myReduce.js +++ b/myReduce.js @@ -3,17 +3,18 @@ function myReduce(arr, callback,initialValue) { var result; - + var i=0; if(initialValue != undefined){ result=initialValue; } else { result=arr[0]; + i++; } if(arr.length > 0) { -for(var i=1 ;i < arr.length ;i++){ +for( ;i < arr.length ;i++){ result= callback(result, arr[i],i,arr); } }