baraa baraa - 2 months ago 5x
Javascript Question

Split number from 1 to N into more than one section

I need to split a number to number of sections

I want number of sections, between each consecutive numbers in each section the number of the sections

For example

If the given number is

and the number of sections is

then need to split numbers 1 to 20 into three sections.

So the result will be

section 1 =
// between any

section 2 =

section 3 =

I'm using javascript to do this.... any idea

Sorry for my bad English


You could use two nested for loops and count the sections and the value to max.

function getSection(sections, max) {
    var value, result = [], i;
    for (i = 1; i <= sections; i++) {
        for (value = i; value <= max; value += sections) {
            result[i - 1].push(value);
    return result;

console.log(getSection(3, 20));