R
R
Ruslan2020-10-20 15:54:51
JavaScript
Ruslan, 2020-10-20 15:54:51

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

3 answer(s)
A
Absolute138, 2020-10-20
@Lorelin

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

U
umid, 2020-10-20
@umidnazarov27

you can do it with an array

for (let i = 1; i < 100; i++) {
    array.push(i)
}

or so the result is the same
[...Array(99).keys()].map(i => i+1)

F
frolandr, 2020-10-20
@frolandr

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 question

Ask a Question

731 491 924 answers to any question