Skip to main content
deleted 4 characters in body
Source Link
StackByMe
  • 6.6k
  • 22
  • 31

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n)); // EDIT: // By making it into a function you can call it with dynamic lengths function makeNumArr(num) { return new Array(num).fill("").map((_, i) => i + 1); } let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10);

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

EDIT: By making it into a function you can call it with dynamic lengths

const makeNumArr = num => new Array(num).fill("").map((_, i) => i + 1); let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10); console.log(fiveArr); console.log(tenArr);

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n)); // EDIT: // By making it into a function you can call it with dynamic lengths function makeNumArr(num) { return new Array(num).fill("").map((_, i) => i + 1); } let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10);

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n));

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

EDIT: By making it into a function you can call it with dynamic lengths

const makeNumArr = num => new Array(num).fill("").map((_, i) => i + 1); let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10); console.log(fiveArr); console.log(tenArr);

added 269 characters in body
Source Link
StackByMe
  • 6.6k
  • 22
  • 31

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n)); // EDIT: // By making it into a function you can call it with dynamic lengths function makeNumArr(num) { return new Array(num).fill("").map((_, i) => i + 1); } let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10);

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n));

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n)); // EDIT: // By making it into a function you can call it with dynamic lengths function makeNumArr(num) { return new Array(num).fill("").map((_, i) => i + 1); } let fiveArr = makeNumArr(5); let tenArr = makeNumArr(10);

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 
added 323 characters in body
Source Link
StackByMe
  • 6.6k
  • 22
  • 31

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based zoso you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n));

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based zo you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n));

You can use the new Array(60); to make an array of length 60. Then append the fill("") to fill they array with empty strings. After that you can map() the index of each item to its value.

(Be aware that the index is zero based so you need to add one).

let arr = new Array(60).fill("").map((_, i) => i + 1); arr.forEach(n => console.log(n));

Detailed:

// Create array of length 60 let arr = new Array(60); // Fill the whole array with a value, i chose empty strings let filledArr = arr.fill(""); // Replace every value with it's index plus one. Index starts at 0 that's why we add one let numArr = filledArr.map((_, i) => i + 1); 
Source Link
StackByMe
  • 6.6k
  • 22
  • 31
Loading