Jump to content
Zerivo

Loop in Loop with Array

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...