+ 1
Please indicate what is the mistake I am doing here
for(let num=2; num<10; num%=2) { if (num===1) alert(num); }
7 Answers
+ 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
+ 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.
+ 1
Aradhay Mathur if we wanna write only one statement in an if condition, we can avoid curly braces.
+ 1
maf yes i think he should increment by one and start with 0
+ 1
I am sorry to mention that I want to print prime numbers less than 10
+ 1
for(var num=0;num<10;num++){num==1?alert(num):''
}
0
Thanks