Jump to content

Loop in Loop with Array


Zerivo

Recommended Posts

Quote

<head><script type="text/javascript">
function AddInfo() {
var text="";var txt=""; var IF="";
var NL=1;var NR=1;
for (NL = 1; NL < 3; NL++){
for (NR = 1; NR < 3; NR++){
      if (NR==1) {CR=["T1","C1"]}
 else if (NR==2) {CR=["T2","C2"]}
 else if (NR==3) {CR=["T3","C3"]}
    text += "L"+NL+': '+CR[0]+'&'+CR[1]+"---"; }
txt += text+"<br>"; }
document.getElementById("Test").innerHTML = txt; }
</script></head>
<body onload="AddInfo()">
<div id="Test"></div>
</body>

filer : loop in loop fail.html

I was try make loop in loop with array. it should be like:
L1: T1&C1---L1: T2&C2---L1: T3&C3---
L2: T1&C1---L2: T2&C2---L2: T3&C3---
L3: T1&C1---L3: T2&C2---L3: T3&C3---

 

But I got other than that i thought, so what did i wrong?

Edited by Zerivo
Link to comment
Share on other sites

You are duplicating the last output, and you require break on last loop where nr == 3.

            function AddInfo() {
                var text = "";
                var txt = "";
                // var IF = "";
                var NL = 1;
                var NR = 1;
                for (NL = 1; NL <= 3; NL++) {
                    for (NR = 1; NR <= 3; NR++) {
                        if (NR == 1) {
                            CR = ["T1", "C1"]
                        }
                        else if (NR == 2) {
                            CR = ["T2", "C2"]
                        }
                        else if (NR == 3) {
                            CR = ["T3", "C3"]
                        }
                        text += "L" + NL + ': ' + CR[0] + '&' + CR[1] + "---";
                        if (NR == 3) {
                            text += "<br>"
                        }
                    }

                }
                txt = text + "";
                document.getElementById("Test").innerHTML = txt;
            }

Also it will never loop through 3rd, because its restricted to looping (< 3) to 2 only.

Edited by dsonesuk
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...