lundi 19 juillet 2021

How do i display the outcome of my for loop with if statement in an html tag like a paragraph

So the first for loop is what i had for a default console.log and the second on is the one i tried to do it with, but no good outcome sadly.

for (let i = 1; i <= 100; i++) {
    if (i % 15 == 0) console.log("FB");
    else if (i % 3 == 0) console.log("F");
    else if (i % 5 == 0) console.log("B");
    else console.log(i);
}

for (let i = 1; i <= 100; i++) {
    if (i % 15 == 0) document.getElementById("fizzbuzz").innerHTML = "FB";
    else if (i % 3 == 0) document.getElementById("fizzbuzz").innerHTML = "F";
    else if (i % 5 == 0) document.getElementById("fizzbuzz").innerHTML = "B";
    else document.getElementById("fizzbuzz").innerHTML = i;
}
<div class="container">
    <h2 class="text-light">Opdracht 2</h2>
    <p class="text-light" id="fizzbuzz"></p>
</div>

Aucun commentaire:

Enregistrer un commentaire