Why code doesn't display text on DOM?

I would like to display the text from this code on DOM but is just not working.

let rands = [];
let count = 0;
const size = 5;

while(count < size) {
    rands.push(Math.round(Math.random() * 10));
    count++;
    rands = "The current size of the array is" + count;
}
document.getElementsByTagName("P").innerHTML = rands;



getElementsByTagName returns an HTMLCollection , like an array. You can’t set innerHTML on an array-like collection because it’s not an HTML element, you need to loop over the array to access the individual elements.

In addition to @DanCouper’s feedback, you’ll run into issues here with using the same variable name for two different things.

Like DanCouper just pointed out. If your document have only one paragraph, then that paragraph is

document.getElementsByTagName("p")[0]

Also, there are another error in your code. The variable rands is supposed to be an array. You reset it to String in the loop at this line:

rands = "The current size of the array is" + count;

I suppose you want to console.log() that line instead of set it to rands?

thanks very useful much more clear now