I was just starting with codewars challenges, one of them is "Generate range of integers" problem. I managed to solve problem with for loop, but i was wondering if i could solve this problem in one line?
I have tried using fill and map methods combined with while and do-while loops. The problem is I am unable get the first number (starting min) inside my returned array. The first time while loop evaluates to true, it returns minimum incremented by step value.
function generateRange(min,max,step) {
return Array(Math.floor((max - min) / step ))
.fill()
.map(() => {while(min <= max) {return min+=step;}});
}
console.log(generateRange(0, 10, 2));
I was expecting number from min to max, with step within them. min = 0, max = 10, step = 2, result => [0,2,4,6,8,10] but i get all the numbers without the first starting minimum [2,4,6,8,10].