+ 2
For loop
Are document.getElementById are alway written outside of the executed loop due to the quotation (" ") in the variables text? I thought they were usually written inside the executed loop for example: <p id="demo"><p> <script> var text = " "; (<--- talking about this) var food = ["chicken", "salad", "crouton"]; var a; for (a = 0; a < food.length; a++) { text += "I love" + food[a] + "<br>"; } document.getElementById("demo").innerHTML = text; </script>
1 Odpowiedź
+ 4
Both is possible, you could also do :
var food = ["chicken", "salad", "crouton"];
for (var a=0; a<food.length; a++) {
document.getElementById("demo").innerHTML += "I love " + food[a] + "<br>";
}
Gives you the exact same result.