Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)}






Expand Down
4 changes: 3 additions & 1 deletion myEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

function myEach(arr, callback) {

// CODE INSIDE HERE //

for (var i = 0; i < arr.length; i++)
callback(arr[i], i, arr);

}

Expand Down
6 changes: 5 additions & 1 deletion myMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}

Expand Down
21 changes: 17 additions & 4 deletions myReduce.js
Original file line number Diff line number Diff line change
@@ -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;
}

/*
Expand Down