function sumOfMulti(){
var sum = 0;
for (var x = 0; x < 100; x++){
if (x % 3 === 0 || x % 5 === 0)
{
document.getElementById("list").innerHTML = x;
sum += x; //sum = sum + x
}
}
alert(sum);
};
hi guys, I want to print each and every value of x which meets the condition in the loop
but it only gives me the last value of them
Javascript does't work this way? will I have to store the values in array first? =( thanks
You could assign it with keeping the old content.
document.getElementById("list").innerHTML += x + '<br>';
function sumOfMulti() {
var sum = 0;
for (var x = 0; x < 100; x++) {
if (x % 3 === 0 || x % 5 === 0) {
document.getElementById("list").innerHTML += x + '<br>';
sum += x;
}
}
}
sumOfMulti();
<div id="list"></div>
The same with creating new nodes.
function sumOfMulti() {
var sum = 0;
for (var x = 0; x < 100; x++) {
if (x % 3 === 0 || x % 5 === 0) {
document.getElementById("list").appendChild(document.createTextNode(x));
document.getElementById("list").appendChild(document.createElement('br'));
sum += x;
}
}
}
sumOfMulti();
<div id="list"></div>