Answer the question
In order to leave comments, you need to log in
How to fill an empty array with your own values?
Hello, I'm just starting to learn JS, please help in the task and explain ..
I need to create an empty array and make a counter that will fill it with numbers from n to m and do it up to a certain lenghth
Answer the question
In order to leave comments, you need to log in
let arr = []; // Мне нужно создать пустой массив
let count = (n, m , l)=>{ //и сделать счетчик
while(n <= m && arr.length < l){ // от n до m И делать это до определенного length
arr.push(n++); // будет его заполнять числами
}
}
count(3, 17, 8); // n, m, length
console.log(arr); // [3, 4, 5, 6, 7, 8, 9, 10]
console.log(arr.length); // 8
you can do it with an array
for (let i = 1; i < 100; i++) {
array.push(i)
}
[...Array(99).keys()].map(i => i+1)
Through for did:
let arr = []; //Создать пустой массив
function fill(n, m, len) { //Заполнить массив числами в промежутке от n до m,
for (let i = 0; i < len; i++) { //ограничить длиной len
if (n <= m) {
arr.push(n);
n++;
}
}
return arr;
}
console.log(fill(-10, 8, 15));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question