...And the good! Seems correct but is not


#1

Hi! For this exercise, I wrote the following code, when it prints, it seems correct. (1 2 3Fizz 4 5Buzz 5Fizz...and etc. and even the 15 I got it right "FizzBuzz") But it says "Oops, try again. It looks like you printed out the wrong number of items." I can't see why. Pls help! Thanks!

for (var i=1; i<21; i++) {
console.log(i);
if(i%3===0 && i%15!==0) {
console.log("Fizz");
} else if(i%5===0 && i%15!==0) {
console.log("Buzz");
} else if(i%15===0) {
console.log("FizzBuzz");
}

};


#2

you shouldn't log the number when its fizz, buzz or fizzbuzz

if you really want to do it right, write the code in such a way that we can easily expand the program to give all fizzbuzz for 1 till 100 (or even more)


#3

Hey thanks for reminding I changed the code to the following and it worked. Thanks!

for (var i=1; i<21; i++) {
if(i%3===0 && i%15!==0) {
console.log("Fizz");
} else if(i%5===0 && i%15!==0) {
console.log("Buzz");
} else if(i%15===0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
};


#4

i am going to be rude, your program is a good example of a poorly designed program. Why? Because if we where to do the following:

for (var i=1; i<100; i++)

your code stops working. Yes, i know its outside the scope of the exercise. But the design of your program doesn't make it very flexible. A good design program is very important.


#5

Hey @stetim94 it's fine that you pointed out. But actually I've tried putting in several variables including 31, 41, and 100 as well. It seems to work from what I saw on the screen to the right.

Can you please tell me where you saw it won't work?

Thanks!


#6

i am so sorry, this is my mistake. Your program is fine.

It is quit common to check if a number is divisible by 3 and 5 first, given this saves a few conditions, but i got a bit confused by your code

My apologizes, again


#7

It's OK :slight_smile: No worries!


#8

Just a reminder @microsolver52570, your code on post 3 should be "formatted". Just pointing that out because some moderators are strict about it. (If you don't know how to do it surround the code block with 3 backticks)
It makes it look from this:
var x = 1;
to this:
var x = 1;


#9

@bobbricks7 Sure! It does seem easier to read.


#10

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.