+ 1
Why do we get the same output?
var i,j,x=0; for(i=0;i<10;i++){for(j=0;j<=2;j++){x++;} console.log(x);} //Output: 3 6 9 12 15 18 21 24 27 30 var i,j,x=0; for(i=0;i<10;i++){for(j=0;j<=2;j++){++x;} console.log(x);} //Output: 3 6 9 12 15 18 21 24 27 30
4 Answers
+ 2
Let's See
var i,j,x = 0;
for(i = 0 ; i < 10; i++) --Top Loop
for(j = 0; j <= 2; j++) -- Bottom
x++;
*The top loop runs 10 times
*The bottom loop runs 3 times.
Each time the top loop runs, the bottom loop "increments x by 1" 3 times.
Thereby making it same as x = x+3;
So after the top loop runs the first time, the bottom loop adds 3 to x, making it 0+3 = 3.
So in a nutshell,
============
Each time the top loop runs, x gets the value of (i+1)*3
In this context, the 2 loops do the same thing.
+ 2
You're confused by the difference between ++x and x++ right?
+ 1
I meant how .. could you explain it for me?
0
I know the difference between them .. ++x means increase by 1 then return x value .. x++ return x value then increase by 1 .. I want explain this with numbers to better understanding I know what is their meanings but I confuse the use of them