vendredi 30 novembre 2018

How to output array of numbers by setInterval Math.floor Math.random

I have multiple html elements <h2> and </b> with the classname: randomgen.

function generateRandomNumber outputs one random number between 1 and 100 to each element:

function generateRandomNumber(min_value, max_value) {

    var random_number = Math.random() * (100 - 1) + 1;
    return Math.floor(random_number);
}

document.getElementsByClassName("randomgen")[0].innerHTML = generateRandomNumber();

Would it be posible to output multiple generateRandomNumber(); in an array, instead of identifying each [x] for the class randomgen.

setInterval(function () {

    document.getElementsByClassName("randomgen")[0].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[1].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[2].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[3].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[4].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[5].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[6].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[7].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[8].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[9].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[10].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[11].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[12].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[13].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[14].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[15].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[16].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[17].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[18].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[19].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[20].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[21].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[22].innerHTML = generateRandomNumber();
    document.getElementsByClassName("randomgen")[23].innerHTML = generateRandomNumber();

}, 8000);




Aucun commentaire:

Enregistrer un commentaire