Created
December 20, 2017 15:14
-
-
Save stevenpray/aeb8542492920ea3115cca87980c5717 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* @param {Array} arr | |
* @param {Function} cb | |
* @returns {Array} | |
*/ | |
function map(a, b) { | |
const result = []; | |
a.forEach(x => result.push(b(x))); | |
return result; | |
} | |
// var array1 = [1, 4, 9, 16]; | |
// console.log(array1.map(x => x * 2)); | |
// console.log(map(array1, x => x * 2)); | |
function reduce(arr, reducer, initial) { | |
let i = 0; | |
if (initial === undefined) { | |
initial = arr[0]; | |
i = 1; | |
} | |
while (i < arr.length) { | |
initial = reducer(initial, arr[i]); | |
i++; | |
} | |
return initial; | |
} | |
const reducer = (a, c) => a + c; | |
const array2 = [2, 2, 3, 4]; | |
console.log(array2.reduce(reducer)); | |
console.log(reduce(array2, reducer)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment