skip to Main Content

I have a peice of code trying to make a list of 25 average numbers 1-25 and they are not showing on the page.

window.onload = function() {
  const alt = [];
  const avgRate = 3; 
  for (let i = 0; i = 25; i++) {
    alt.push(Math.random()*25)
  }
  console.log(alt); 
  document.getElementById('output').innerHTML = alt;
}
<p id="output"></p>

I expected the lists to appear on the page but did not see them Can someone help me with this?

4

Answers


  1. I think maybe it’s supposed to be i != 25 and not i = 25?
    I’m pretty sure that i = 25 means the condition for continuation, not the condition for stopping.

    Login or Signup to reply.
  2.     for (let i = 0; i < 25; i++) {
      alt.push(Math.random()*25)
    }
    

    the condition in the for loop should run the code 25 times and add elements to it

    Login or Signup to reply.
  3. This is because you create a i variable for your for condition and directly affect 25 to the value, try to set a stop condition like i < 25

    window.onload = function() {
      const alt = [];
      const avgRate = 3; 
      for (let i = 0; i < 25; i++) {
        alt.push(Math.random()*25)
      }
      document.getElementById('output').innerHTML = alt;
    }
    <p id="output"></p>
    Login or Signup to reply.
  4. It is causing due to the looping issues you can use i<=25 and also alt.push(Math.floor(Math.random()*25)) to avoid the leading decimal values.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search