๐ŸšงArray.list(n)

๐Ÿ’พ replit

// โญ๏ธ Array.list(n)
Array.list = function(n, {
    start = 0,
    step = 1,
} = {}) {

    // initialize
    let i = 0;                  // integer index
    let value = start;          // array item value
    let arr = Array(n);         // empty array

    while (i < n) {
        arr[i] = value;         // set item value
        value += step;          // next value (increment by `step`)
        i += 1;                 // next index
    }

    return arr;
};

๐Ÿ’ˆ็ฏ„ไพ‹๏ผš

Array.list(5),                          // [ 0, 1, 2, 3, 4 ]
Array.list(5, { start: 2 }),            // [ 2, 3, 4, 5, 6 ]
Array.list(5, { start: 2, step: 3 }),   // [ 2, 5, 8, 11, 14 ]
Array.list(5, { step: -1 }),            // [ 0, -1, -2, -3, -4 ]

Last updated