Search code examples
javascripthtmlfor-loopdomnodes

How do I print a repeated piece of javascript to a HTML document using a for loop?


Self-teaching amateur here trying to create a worksheet to help students practice simultaneous equations. I am struggling with how to run the code below repeatedly to generate multiple questions.

I think the issue is with the [i] here

document.getElementsByClassName("question")[i].getElementsByClassName("part")[n].innerHTML

Could someone please explain to me why using the for loop variable to repeatedly write to HTML like this does not work and how I can fix it?

Thanks very much for your help.

<div class="question">
  <ul>
    <li class="part"></li>
    <li class="part"></li>
    <li class="part"></li>
  </ul>
</div>

<div class="question">
  <ul>
    <li class="part"></li>
    <li class="part"></li>
    <li class="part"></li>
  </ul>
</div>



for (i = 0; i < 5; i++){
    var n = 12

    x = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))
    y = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))
    z = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))

    m = 20
    a = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    b = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    c = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    d = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    e = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    f = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    g = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    h = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    i = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))

    rhs1 = a*x + b*y + c*z
    rhs2=  d*x + e*y + f*z
    rhs3 = g*x + h*y + i*z

    document.getElementsByClassName("question")[i].getElementsByClassName("part")[0].innerHTML= a + " x + " + b + " y + " + z + " z = " + rhs1;
    document.getElementsByClassName("question")[i].getElementsByClassName("part")[1].innerHTML= d + " x + " + e + " y + " + f + " z = " + rhs2;
    document.getElementsByClassName("question")[i].getElementsByClassName("part")[2].innerHTML= g + " x + " + h + " y + " + i + " z = " + rhs3;

  }

Solution

  • The problem that you are having is you are referring to i as an incremental value in your for loop, but you then change it's value to the value of a calculation.

    I changed it to cntr.

    for (cntr=0; cntr<5; cntr++){
    var n = 12
    
        x = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))
        y = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))
        z = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*n))
    
        m = 20
        a = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        b = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        c = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    
        d = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        e = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        f = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    
        g = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        h = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
        i = (Math.random()<0.5? 1:-1)*(Math.ceil(Math.random()*m))
    
        rhs1 = a*x + b*y + c*z
        rhs2=  d*x + e*y + f*z
        rhs3 = g*x + h*y + i*z
    
        document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[0].innerHTML= a + " x + " + b + " y + " + z + " z = " + rhs1;
        document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[1].innerHTML= d + " x + " + e + " y + " + f + " z = " + rhs2;
        document.getElementsByClassName("question")[cntr].getElementsByClassName("part")[2].innerHTML= g + " x + " + h + " y + " + i + " z = " + rhs3;
    
      }