javascript - print numbers between 1- 20 using rules -


i going through codeacademys javascript tutorials new it. tutorial asks following:

print out numbers 1 - 20.
the rules:
- numbers divisible 3, print out "fizz".
- numbers divisible 5, print out "buzz".
- numbers divisible both 3 , 5, print out "fizzbuzz" in console.
- otherwise, print out number.

here code:

for (i=1; i<=20; i++) {  if(i%3==0) {     console.log("fizz"); }  if(i%5==0){     console.log("buzz"); }else if (i%5==0 && i%3==0) {     console.log("fizzbuzz"); } else {     console.log(i); }  } 

i getting error saying printing out wrong number of items, know why is?

when value divisible 3 , not 5, on first if statement "fizz" printed.

then on second if statement, last else hit number printed. need change if(i%5==0) else if.

however there problem when (i%5==0 && i%3==0) else if never hit. can fix putting first comparison , changing output fizzbuzz.

like this:

for ( = 1; <= 20; i++) {     if (i % 5 === 0 && % 3 === 0) {         console.log("fizzbuzz");     } else if (i % 3 === 0) {         console.log("fizz");     } else if (i % 5 === 0) {         console.log("buzz");     } else {         console.log(i);     } }; 

make sure understand why fixes issue before move on make same mistake again.

add comment if me explain clearer if struggling work out why have gone wrong.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -