baraa - 1 year ago 63
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

`20`
and the number of sections is
`3`

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

So the result will be

section 1 =
`[1,4,7,10,13,16,19]`
// between any

section 2 =
`[2,5,8,11,14,17,20]`

section 3 =
`[3,6,9,12,15,18]`

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

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++) {
result.push([]);
for (value = i; value <= max; value += sections) {
result[i - 1].push(value);
}
}
return result;
}

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download