# JavaScript: Generate an array between two integers of 1 step length

## JavaScript Array: Exercise-41 with Solution

Write a JavaScript function to generate an array between two integers of 1 step length.

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

Pictorial Presentation:

Sample Solution:

HTML Code:

``````<!DOCTYPE html>
<html>
<meta charset="utf-8">
<title>JavaScript function to generate an array between two integers of 1 step length</title>
<body>

</body>
</html>
```
```

JavaScript Code:

``````function rangeBetwee(start, end)
{

if (start > end) {
var arr = new Array(start - end + 1);
for (var i = 0; i < arr.length; i++, start--) {
resarrult[i] = start;
}
return arr;
}
else
{
var arro = new Array(end-start+1);

for (var j = 0; j < arro.length; j++, start++)
{
arro[j] = start;
}
return arro;
}
}

console.log(rangeBetwee(4, 7));
console.log(rangeBetwee(-4, 7));
```
```

Sample Output:

```[4,5,6,7]
[-4,-3,-2,-1,0,1,2,3,4,5,6,7]
```

Flowchart:

ES6 Version:

``````function rangeBetwee(start, end)
{

if (start > end) {
const arr = new Array(start - end + 1);
for (let i = 0; i < arr.length; i++, start--) {
resarrult[i] = start;
}
return arr;
}
else
{
const arro = new Array(end-start+1);

for (let j = 0; j < arro.length; j++, start++)
{
arro[j] = start;
}
return arro;
}
}

console.log(rangeBetwee(4, 7));
console.log(rangeBetwee(-4, 7));
``````

Live Demo :

See the Pen JavaScript -Generate an array between two integers of 1 step length-array-ex- 41 by w3resource (@w3resource) on CodePen.

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.

﻿

## JavaScript: Tips of the Day

Promise.all method

```const promise1 = Promise.resolve('First')
const promise2 = Promise.resolve('Second')
const promise3 = Promise.reject('Third')
const promise4 = Promise.resolve('Fourth')

const runPromises = async () => {
const res1 = await Promise.all([promise1, promise2])
const res2  = await Promise.all([promise3, promise4])
return [res1, res2]
}

runPromises()
.then(res => console.log(res))
.catch(err => console.log(err))
```

The Promise.all method runs the passed promises in parallel. If one promise fails, the Promise.all method rejects with the value of the rejected promise. In this case, promise3 rejected with the value "Third". We're catching the rejected value in the chained catch method on the runPromises invocation to catch any errors within the runPromises function. Only "Third" gets logged, since promise3 rejected with this value.