+ 1

Please indicate what is the mistake I am doing here

for(let num=2; num<10; num%=2) { if (num===1) alert(num); }

20th Nov 2020, 10:38 AM
Dev Sharma
Dev Sharma - avatar
7 ответов
+ 2
The if statement has no braces "{}" the code should be ________________________``` for(let num = 2; num<10; num%=2){ if(num===1){ alert(num) } } --------------------------------------------- ``` And the loop will run forever and no alerts will be shown. Replace num%=2 to num+=1 and let num=2 to let num=0
20th Nov 2020, 12:01 PM
Aradhay Mathur
Aradhay Mathur - avatar
+ 2
Dev Sharma i dont understand the logic of your for loop. num is starting from 2 and on each iteration, you assign the value of num % 2 to num so if num is 2, 2 % 2 would be 0, then 0 % 2 would be 0, then again 0, it wouldn't go to 10, and if it wouldn't be equal to 1 so that it wont alert(num) % means remainder, so 4 % 3 = 1.
20th Nov 2020, 12:07 PM
maf
maf - avatar
+ 1
Aradhay Mathur if we wanna write only one statement in an if condition, we can avoid curly braces.
20th Nov 2020, 12:05 PM
maf
maf - avatar
+ 1
maf yes i think he should increment by one and start with 0
20th Nov 2020, 12:09 PM
Aradhay Mathur
Aradhay Mathur - avatar
+ 1
I am sorry to mention that I want to print prime numbers less than 10
20th Nov 2020, 12:33 PM
Dev Sharma
Dev Sharma - avatar
+ 1
for(var num=0;num<10;num++){num==1?alert(num):'' }
21st Nov 2020, 4:01 PM
SAN
SAN - avatar
0
Thanks
21st Nov 2020, 5:11 PM
Dev Sharma
Dev Sharma - avatar