user129963
user129963

Reputation: 13

How come my js array is only showing the last one?

I don’t know if this is similar or not to other topics. I’m basically testing my code at this point and I have no idea why it’s not working.

let p = document.querySelector("#demo");
let m = document.querySelector("body");
let msg = "";
let arr1 = [23, 55, 63, 88, 95];
for (i = 0; i < arr1.length; i++){
   msg = arr1[i] + "<Br>";
}
p.innerHTML = msg;

Upvotes: 0

Views: 43

Answers (1)

Get Off My Lawn
Get Off My Lawn

Reputation: 36311

We could just remove the loop all together and do a join:

let p = document.querySelector("#demo");
let arr1 = [23, 55, 63, 88, 95];

p.innerHTML = arr1.join('<br>');
<p id="demo"></p>

Upvotes: 1

Related Questions