- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › Code Academy help…
Hi guys and girls
I am just working my way through a Javascript section on Code Academy and I’m really liking it up to now, however I’m stuck on this question;
**The President has notified you that he only wants you to square the ODD numbers between 100 and 150.**
**Add an if…else statement to your for loop block that looks at each number. If it is odd, print the square of the number. Otherwise, just print the number.**
I had this but I think I’m way off;
for (var i = 100; i <= 150; i++) {
var square = i % i;
if (square) {
print (square);
}
else {
print (square)
}
}
I think you want to do
if(i % 2) {
print i*i;
}
It checks to see if there’s a remainder from i divided by 2. If there is then i is odd. If it is odd then square it.
Thanks so much :) I had to amend it a tiny bit but you were right;
for (var i = 100; i <= 150; i++) {
if (i % 2) {
print (i * i);
}
else {
print (i);
}
} //End of for statement
Sorry! I was on my phone at the time or I would have typed the whole solution out :)
It’s okay :) you gave me what I needed really. Helped me get more used to the syntax adding the brackets etc. Really liking this JavaScript atm