JenMorgan / js-learning

0 stars 0 forks source link

Write a JavaScript function to create a specified number of elements with pre-filled numeric value array #35

Open kartamyshev opened 4 years ago

kartamyshev commented 4 years ago
fill(6, 0)); // [0, 0, 0, 0, 0, 0]
fill(4, 11)); // [11, 11, 11, 11]
JenMorgan commented 4 years ago
function fill(quantity, value) {
    let array = [];
    for (let i = 0; i<quantity; i++) {
        array[i] = value;
    }
    return array;
}
JenMorgan commented 4 years ago
function fill(quantity, value) {
    return Array.from({length: quantity}, () => {
        return value;
    })
}