In python3x I'm using a simple code:
for i in range(1, 10):
print(i)
I tried using Array function on NodeJS/JavaScript:
const LIST = [];
const Range = Array(10).fill("0, 10", 0, 10)
LIST.push(Range);
console.log(LIST);
But it seems it just gonna give output:
[
[
'0, 10', '0, 10',
'0, 10', '0, 10',
'0, 10', '0, 10',
'0, 10', '0, 10',
'0, 10', '0, 10'
]
]
How do I make it gives output from 1 to 10?
Why... not just use a plain for loop?
for(let i = 1; i < 10; i++) {
console.log(i);
}
Array() fills the array with nothing but gives it a certain length, so filling it with null makes it [null, null, ...]. Mapping the array is similar to mapping in python, you loop through each item in the array and return the current index + 1. That creates an array [1, 2, 3, 4 ..., 10]
const Range = Array(10).fill(null).map((_, i) => i + 1);
console.log(Range);