0

i have this for loop:

for (var i = 0; i < pictures.length; i++) {
    var mod = i % 4;
    alert(mod); // first
    //get the current row
    if (mod == 0)
    {
        alert(mod); //second
        tableBody.innerHTML += "<tr>";
    }
    tableBody.innerHTML += "<td><img style='width:146px; height:146px;' src='" + pictures[i].source + "'></td>";
    if (mod == 0)
        tableBody.innerHTML += "</tr>";
}

how come my first alert shows the result of the math calculation inside "mod" variable, and the second alert show always zero ??

1
  • Because your 2nd alert is within an if (mod == 0) statement... Commented Feb 2, 2014 at 10:10

3 Answers 3

2

Because you execute the second alert if and only if mod == 0.

Sign up to request clarification or add additional context in comments.

2 Comments

you are right, but it does enter my if statement event when its not == 0
you are welcome @AsafNevo :) also I think it's better to use === here instead of == (although it doesn't matter much here, but it's more conventional). Please consider marking as accepted answer if you see it so. tnx
0

The second alert will show 0 because you have it inside an if() statement which checks if mod == 0

Comments

0

C'mon your second alert is inside if() statement. It will get executed only when mod=0. If mod=0, it will enter if loop and execute the statements there. Your if statement contains alert(mod). So, it will print the value of mod which is equal to zero. And in your first alert, it will calculate the value of mod and will print whatever is the value of variable mod(irrsepective of the fact whether it is zero).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.