baraa 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

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

Answer Source

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));

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