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
37 changes: 25 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
8 changes: 4 additions & 4 deletions myEach.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

/*
Expand Down
8 changes: 5 additions & 3 deletions myMap.js
Original file line number Diff line number Diff line change
@@ -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;
}

/*
Expand Down
20 changes: 17 additions & 3 deletions myReduce.js
Original file line number Diff line number Diff line change
@@ -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.
Expand Down