7 kyu
Sequence generator
5,046czterystaczwarty
Description:
Implement function sequence
, which returns new n
-size Array filled according to pattern
.
pattern
may be:
- a
function
that takes two:(element, index)
, one:(element)
or any arguments (similar tomap
function), then filled running this function, in other words: function describes sequence, - number, string or any other object, then filled by copying, this object
n
-times.
Examples:
sequence(3, 4); // [4, 4, 4]
sequence(5, []); // [[], [], [], [], []]
sequence(2, "s"); // ["s", "s"]
sequence(5, (x, idx) => idx%2) // [0, 1, 0, 1, 0];
sequence(10, (x, idx) => idx+1) // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Note: Sequences are great to work with functional methods like map
, reduce
, forEach
, every
or any
. For example:
// sum of numbers 1-10
let sum = sequence(10, (x, idx) => idx+1).reduce((sum, num) => sum + num);
Be careful with long sequences. They are just arrays, every element is created when function is called.
For lazy sequences (elements created when needed) use Iterator.
Arrays
Functional Programming
Fundamentals
Similar Kata:
Stats:
Created | Feb 10, 2016 |
Published | Feb 10, 2016 |
Warriors Trained | 6450 |
Total Skips | 34 |
Total Code Submissions | 26872 |
Total Times Completed | 5046 |
JavaScript Completions | 5046 |
Total Stars | 31 |
% of votes with a positive feedback rating | 83% of 369 |
Total "Very Satisfied" Votes | 271 |
Total "Somewhat Satisfied" Votes | 74 |
Total "Not Satisfied" Votes | 24 |