I am give start and the end and the size, I am required to generate array as in illustration
when
start = 1
end = 100
size = 10
the expected result is
[
[10,20],[20,30],[30,40],[40,50],[50,60],[60,70],[70,80],[80,90],[90,100]
]
UPDATE
second example
When
start = 1
end = 100
size = 10
expected result
[[5,10],[10,15],[15,20],[20,25],....[90-95],[95,100]]
I have tried with,
var start = 1;
end = 100
size = 10;
result = []
for (var i = start; i <= end; i+=size) {
result.push([i, i + size-1])
}
console.log(JSON.stringify(result))
>Solution :
Simply changing [i, i + size-1] to [i - 1, i + size-1] seemed to work just fine. The first element in the array doesn’t match your example, but it can easily be removed. Full code below.
var start = 1;
end = 100
size = 10;
result = []
for (var i = start; i <= end; i += size) {
if (i <= start * 10) {
end += 10;
continue;
}
result.push([i - start, i + 9 - (start - 1)])
if (i > 200) break
}
console.log(JSON.stringify(result))