w3resource

JavaScript: Generate an array of specified length, the content of the array is integer numbers, increase by one from starting

JavaScript Array: Exercise-40 with Solution

Generate Integer Range Array

Write a JavaScript function to generate an array of integer numbers, increasing one from the starting position, of a specified length.

Test Data :
console.log(array_range(1, 4));
[1, 2, 3, 4]
console.log(array_range(-6, 4));
[-6, -5, -4, -3]

Visual Presentation:

JavaScript: Generate an array of specified length, the content of the array is integer numbers, increase by one from starting

Sample Solution:

JavaScript Code:

// Function to generate an array of numbers in a specified range
function array_range(start, len) 
{
  // Create a new array with the specified length
  var arr = new Array(len);

  // Iterate through the array, filling it with values incremented from 'start'
  for (var i = 0; i < len; i++, start++) 
  {
    arr[i] = start;
  }

  // Return the generated array
  return arr;
}

// Output the result of generating an array with a starting value of 1 and a length of 4
console.log(array_range(1, 4));

// Output the result of generating an array with a starting value of -6 and a length of 4
console.log(array_range(-6, 4));

Output:

[1,2,3,4]
[-6,-5,-4,-3]

Flowchart:

Flowchart: JavaScript: Generate an array of specified length, filled with integer numbers, increase by one from starting

ES6 Version:

// Function to generate an array of numbers in a specified range
function array_range(start, len) 
{
  // Create a new array with the specified length using Array.from
  const arr = Array.from({ length: len }, (_, index) => start + index);

  // Return the generated array
  return arr;
}

// Output the result of generating an array with a starting value of 1 and a length of 4
console.log(array_range(1, 4));

// Output the result of generating an array with a starting value of -6 and a length of 4
console.log(array_range(-6, 4));

Live Demo:

See the Pen JavaScript - Generate an array of specified length, the content of the array is integer numbers, increase by one from starting-array-ex- 40 by w3resource (@w3resource) on CodePen.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that generates an array of consecutive integers starting from a given number with a specified length.
  • Write a JavaScript function that uses Array.from() to create a range of integers between two values.
  • Write a JavaScript function that uses a for loop to populate an array with consecutive numbers until the desired length is reached.
  • Write a JavaScript function that validates the starting value and length, returning an error for invalid inputs.

Go to:


PREV : Filter Array Values.
NEXT : Array Between Two Integers.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.