0

Could you briefly explain why the correct answer to this question is 6?

var arr=[5,7,2,6,3]; for(var c=1;c<=5;c+=2);//<--??? var x=arr[3];//6 x=(x%4);//%2 x*=2;// c=(c%x);//%6 alert(arr[c]);

29th Aug 2019, 8:34 AM
H mi
H mi - avatar
3 Answers
+ 2
Let's split it line by line at Line 1 it creates an array And at 2nd line there is a for loop which ends with a semicolon; so the program will iterate var c till it meets the condition. At c=7 the condition returns false.. so the value of c is now 7. At line 3.. var x equals to 6 At line 4.. var x changed to 2 At line 5.. var x is multiplied by 2 (using shorthand operator *= ) hence x=4 At line 6.. var c is changed to 3 (as 7%4) At line 7.. array of 3 is 6. So it prints 6.
29th Aug 2019, 9:03 AM
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ 
ŠœŠ³. ŠšŠ½Š°ŠæšŸŒ  - avatar
+ 1
mmm... i'm just trying uncomplicate as the same way that you have writing first... var arr=[5,7,2,6,3]; for(var c=1; c <= 5 ; c += 2) // c=1 >> 3 >> 5 >> 7 stop var x=arr[3]; // x=6 x=(x%4); // x= 6%4 =2 x*=2; // x= x*2 = 2*2 =4 c=(c%x); // c= 7%4 =3 alert(arr[c]); // arr[3] >> 6
8th Sep 2019, 7:47 PM
Oneill~ŠžŠ½ŠµŠøŠ»Š»~億尼尔~ą¤“ą¤Øą„€ą¤²~Ų§ŁˆŁ†ŪŒŁ„~*ā€Ž
Oneill~ŠžŠ½ŠµŠøŠ»Š»~億尼尔~ą¤“ą¤Øą„€ą¤²~Ų§ŁˆŁ†ŪŒŁ„~*ā€Ž - avatar
0
Sami Khan Thank you for your uncomplicated explanation.
29th Aug 2019, 9:52 AM
H mi
H mi - avatar