javascript - Why no fizzbuzz being printed out? -
this code. not getting fizz buzz printed. getting numbers. explain why? thanks
printout = ""; (var x=1; x < 101 ; x++) { switch(x) { case((x%3) == 0): printout+="\n"+ "fizz" ; break; case((x%5) == 0): printout+="\nbuzz"; break; default: printout+="\n" + x ; break; } } console.log(printout);
you're using switch
statement improperly. each case (value):
supposed run whenever x
equals value
.
to solve problem, remove switch
statement entirely, , substitute if
s each case
:
for (var x = 1; x < 101; x++) { if ((x % 3) == 0) printout += "\n" + "fizz"; else if ((x % 5) == 0) printout += "\nbuzz"; else printout += "\n" + x; }
Comments
Post a Comment