0

Can anyone please tell me why it's answer is 04

for(i=0;i<=5;i++){ i += i; console.log(i); i++; }

26th Jan 2022, 10:16 AM
Coderado
Coderado - avatar
4 Réponses
+ 6
First iteration ( i = 0 ) i += i; // still 0 i++; // becomes 1 i++ // becomes 2 ( in loop construct ) Second iteration ( i = 2 ) i += i; // becomes 4 i++; // becomes 5 i++ // becomes 6 ( in loop construct ) Loop cease repetition cause <i> is now 6, which means the condition i <= 5 no longer satisfy.
26th Jan 2022, 10:26 AM
Ipang
+ 3
console.log() prints its argument in separate line. I got the below result in JS console 0 4
26th Jan 2022, 10:35 AM
Ipang
+ 1
then why my console is showing 0 4 output
26th Jan 2022, 10:31 AM
Coderado
Coderado - avatar
0
First iteration: i=0; Print 0 Second interaction: i=1 i++=2 i+=i Print 4
28th Jan 2022, 4:13 AM
Emmanuel David Miranda
Emmanuel David Miranda - avatar