0

Please anyone explaine me how the ans being 3 not 4. Translated your ans in hindi by google translator, please.

Question What shows in the console? .............................................. var sum = 0; for (var i = 0; i < 5; i++) { = sum += i: if (i 2) { break; } } console.log(sum); ............................................... My ans:4 Right=(3)

25th Sep 2020, 12:58 PM
RD:programmer
RD:programmer - avatar
6 Answers
+ 6
First write code completely u did one mistake in if condition u should write if(i==2) When first time loop will execute i =0 and var sum = 0; Then we will check condition 0<5 which is true so inner part will be execute sum =0+0 becoz both are zero then check if condition If(0==2) this is false then loop will run again here i will increase by 1 so i=1 1<5 condition true so inner will execute sum=i+sum =1+0 so sum =1 then we will check if condition ita false so loop value will increase again i=2 check condition 2<5 condition true so So sum=sum +i (sum=1 and i=2 ) sum=1+2=3 it will assign to sum then we check if condition i was 2 So if(2==2) condition true so loop will break and final for (var i = 0; i < 5; i++) { sum += i // sum=0+0 first time if (i ==2) { break; } }console.log(sum); final Output will be 3
25th Sep 2020, 1:49 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 6
Suberna Das what happened if u have any doubts u can ask here why u pasting my same answer in dm
25th Sep 2020, 2:48 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 6
for (var i = 0; i < 5; i++) { sum += i: } See this loop will work total 5 times first time i=0 then we check condition 0<5 this is true then sum+=i means sum=sum+i sum=0+0 =0 so sum is 0 here now i is increase see third step of loop u write i++ so before i was 0 now i is 1 So sum+=i means sum=sum+ i Sum was 0 and i =1 sum=0+1=1 and this will assign to sum =0 again third step i ++ now i will increase again so i=2 Next sum+=i which is sum=sum+i (sum=1 and i =2 ) Sum=3 Then if condition we check its true so break
25th Sep 2020, 2:54 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
It's not going to run. First fix syntax errors.
25th Sep 2020, 1:03 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
0
Sum =sum+i(sum=1, i=2) Sum=1+2=3 "How value of sum reach to 1, because sum=0;is given. You write also sum=0+0and1+0
25th Sep 2020, 2:49 PM
RD:programmer
RD:programmer - avatar
0
Sum का मान 1 कैसे हो गया आपने तो पहले 0 लिखा था. समझा दीजिए प्लीज।
25th Sep 2020, 2:50 PM
RD:programmer
RD:programmer - avatar