Hello in this code when I am seeing the output, it is not working.
for (var i = 1; i <= 10; i++) {
var x = '2 × ' +i +' = ' +2*i +'<br>';
};
document.getElementById('table').innerHTML = x;
<p id="table"></p>
I expected it to give the text to that paragraph.
This text: 2 × 1 = 2
And upto 2 × 10 = 20
Also there was <br>
for next line.
I tried to declare x outside also but still it is not working:
The modified code inside <script>
var x;
for (var i = 1; i <= 10; i++) {
x = '2 × ' +i +' = ' +2*i +'<br>';
};
document.getElementById('table').innerHTML = x;
<p id="table"></p>
3
Answers
Assuming that you want to have all x values. You need to append them.
Try this:
You’re overwriting
x
on each iteration of the loop.Initialize
x
to an empty string, then use+=
to concatenate to it.