6 kyu
Array#reduce
4,238 of 4,412user578387
Description:
In this kata, you must define the Array.reduce
method.
I have disabled the pre-existing reduce methods.
Here's how it works:
[1,2,3].reduce( function(sum, next){return sum+next}, 0)
// => 6
['a','b','a'].reduce( function(obj, elem){if(!obj[elem]) obj[elem] = 0; obj[elem] += 1; return obj}, {})
// => {'a':2, 'b':1}
Summary: The reduce
method goes through each element of an array, applies the function to whatever the function returned last, and returns the last outcome. On the first iteration, it should pass the initial
value to the function, as well as the first element of the array. If no initial
value is passed, skip to the first element of the array.
Ruby methods should expect a lambda.
Functional Programming
Algorithms
Similar Kata:
Stats:
Created | Sep 11, 2014 |
Published | Sep 11, 2014 |
Warriors Trained | 6714 |
Total Skips | 503 |
Total Code Submissions | 41386 |
Total Times Completed | 4412 |
JavaScript Completions | 4238 |
CoffeeScript Completions | 28 |
Ruby Completions | 184 |
Total Stars | 68 |
% of votes with a positive feedback rating | 85% of 245 |
Total "Very Satisfied" Votes | 186 |
Total "Somewhat Satisfied" Votes | 45 |
Total "Not Satisfied" Votes | 14 |