We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<script> // myReduce function myReduce(array, callback, initValue = 0) { let accumulator = initValue; for (let index = 0; index < array.length; index++) { const element = array[index]; accumulator = callback.apply(null, [accumulator].concat(array[index])) } return accumulator; } const array2 = [1, 2, 3, 4]; const reducer = (accumulator, currentValue) => accumulator + currentValue; // 1 + 2 + 3 + 4 console.log('myReduce1', myReduce(array2, reducer)); // expected output: 10 // 5 + 1 + 2 + 3 + 4 console.log('myReduce2', myReduce(array2, reducer, 5)); // expected output: 15 // myForeach function myForeach(array, callback) { for (let index = 0; index < array.length; index++) { const element = array[index]; callback(element, index); } } var array1 = ['a', 'b', 'c']; myForeach(array1, function (element) { console.log('myForeach', element); }); // myMap function myMap(array, callback) { const res = []; for (let index = 0; index < array.length; index++) { const element = array[index]; res.push(callback(element, index)); } return res; } var array1 = [1, 4, 9, 16]; const map1 = myMap(array1, x => x * 2); console.log('myMap', map1); // myFilter function myFilter(array, callback) { const res = []; for (let index = 0; index < array.length; index++) { const element = array[index]; if (!!callback(element, index)) { res.push(element); } } return res; } var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present']; const filter1 = myFilter(words, word => word.length > 6); console.log('myFilter', filter1); </script>
The text was updated successfully, but these errors were encountered:
No branches or pull requests
The text was updated successfully, but these errors were encountered: