generate range of numbers javascript code example
Example 1: javascript get random number in range
function getRandomNumberBetween(min,max){
return Math.floor(Math.random()*(max-min+1)+min);
}
Example 2: generate random numbers in js
Math.floor((Math.random() * 100) + 1);
Example 3: javascript generate a random integer number in a range of numbers
const min = 1;
const max = 4;
const intNumber = Math.floor(Math.random() * (max - min)) + min;
console.log(intNumber);
Example 4: range javascript
[...Array(5).keys()];
=> [0, 1, 2, 3, 4]
Example 5: generate array range
for (i of range(1, 5)) {
console.log(i);
}
[...range(1, 5)]
Example 6: Use Recursion to Create a Range of Numbers
function rangeOfNumbers(startNum, endNum) {
if (endNum - startNum === 0) {
return [startNum];
} else {
var numbers = rangeOfNumbers(startNum, endNum - 1);
numbers.push(endNum);
return numbers;
}
}