+ 1

Why is my code continue to show result after if

I want the code to give me the result of division by 3, only if id dose divided by it var div = 2852; var res=div/3; if (div%3==0) {alert ("your number can be divided by 3");} document.write("the result is " + res); else { alert("sry it can't"); }

17th Feb 2018, 9:57 AM
Emil Tayeb
Emil Tayeb - avatar
2 Antworten
+ 1
After closing the if block, you write "the result is..." and then you open an else block. That can't work, because you can only open an else block directly after closing an if block. Just move the } after the alert to "the result is...".
17th Feb 2018, 10:14 AM
Moritz Vogel
Moritz Vogel - avatar
+ 1
Thank you for the explanation! This was my first code, and i really enjoyed it.
17th Feb 2018, 10:38 AM
Emil Tayeb
Emil Tayeb - avatar