简体   繁体   中英

Javascript loop inside a loop with if statement

var jaar = 2014 ; 
var dagen = new Array("maandag ", "dinsdag ", "woensdag ", "donderdag ", "vrijdag ", "zaterdag ", "zondag ");
var dag = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]


document.write("<h2>januari</h2> <br> <br>");
for(i = 0 ; i < dagen.length ; i++)
{

    document.write(dagen[i]);

     if( i == 2)
    {
        for(x = 0 ; x < dag.length ; x++)
        document.write(dag[x])
    }
    else()
    {
    }    

}

For some reason my script isnt working, what i wanna do is that my second loop runs when my first loop is at his 2 array point. and i also want that my second loops runs one time. can someone help me ?

else () will cause the syntax error. If you would like to check some condition in else condition, then use

else if(some condition){ }

or simply use

else {

}

I have corrected your code. Please run the code given below.

var jaar = 2014 ; 
var dagen = new Array("maandag ", "dinsdag ", "woensdag ", "donderdag ", "vrijdag ", "zaterdag ", "zondag ");
var dag = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31]


document.write("<h2>januari</h2> <br> <br>");
for(i = 0 ; i < dagen.length ; i++)
{

    document.write(dagen[i]);

     if( i == 2)
    {
        for(x = 0 ; x < dag.length ; x++)
        document.write(dag[x])
    }
    else
    {
    }    

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM