Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Intro to Programming, Extra Credit, Fizzbuzz, please review my code.

Working on the Fizzbuzz challenge. Instructions included.

**Write a program that loops through the numbers 1 through 100. Each number should be printed to the console, using console.log(). However, if the number is a multiple of 3, don't print the number, instead print the word "fizz". If the number is a multiple of 5, print "buzz" instead of the number. If it is a multiple of 3 and a multiple of 5, print "fizzbuzz" instead of the number.

Hint. Use loops and if/else statments. In javascript the % is the modulo, or remainder operator. a % b evaluates to the remainder of a divided by b. 11 % 3 is equal to 2.**

Here is my code. I do not think my last else if is working correctly though.

Any criticism is appreciated. Thanks.

var counter = 100;

for (var counter=100; counter; counter = counter -1) {

    if (counter % 3) {
        console.log("fizz")
    }

    else if (counter % 5) {
        console.log("buzz")
    }

    else if (counter % 3 && counter % 5) {
        console.log("fizzbuzz")
    }

    console.log(counter)
}

3 Answers

Hi Jonathan,

You have to do your multiple of 15 check first. Otherwise, when you're on a multiple of 15, the mod 3 check will be true and you will print "fizz" instead of "fizzbuzz"

So that should be the 1st one and the order of the separate mod 3 and mod 5 checks after that don't matter although the program will run slightly faster if you do mod 3 before mod 5

Also, logging the counter should be in an else block. You only want to log the counter if you're not logging a string, not log the number every time.

You need to check if the mod operation is equal to zero. Like this if (counter % 3 == 0) {

You also can simplify this (counter % 3 && counter % 5) to this (counter % 15 == 0) If it's a multiple of 3 and 5 then it's a multiple of 15.

You also can simplify this (counter % 3 && counter % 5) to this (counter % 15 == 0) If it's a multiple of 3 and 5 then it's a multiple of 15.

I actually think the code is cleaner using (counter % 3 && counter % 5)

Hi James,

I think you could certainly separate them like that but I think you still need to compare to zero for it to work out right.

if (counter % 3 == 0 && counter % 5 == 0)

I probably should have pointed out that I was only suggesting that change as a performance improvement. One modulo operation vs. 2.

I think you could certainly separate them like that but I think you still need to compare to zero for it to work out right.

You are certainly right on that point, as I didn't clarify that.

I probably should have pointed out that I was only suggesting that change as a performance improvement. One modulo operation vs. 2.

That's a micro optimization and as a rule of thumb is generally unhelpful in the vast majority of case, it's much better to worry about reliability and readability.

Jason Anello & James Barnett does this look right guys? It appears to be working now in the Google Chrome Console. Thank you both.

var counter = 100;

for (var counter=100; counter; counter = counter -1) {

    if (counter % 15 == 0) {
        console.log("fizzbuzz")
    }

    else if (counter % 3 == 0) {
        console.log("fizz")
    }

    else if (counter % 5 == 0) {
        console.log("buzz")
    }

    console.log(counter)
}

That's looking pretty good now. Maybe I made a bad call on telling you to do the mod 15. You can change that back depending on how you feel about it.

The only issue I see now is that you're printing every number. You should only print the number if you're not printing one of those strings. You need to add an else block.

else {
  console.log(counter)
}

This way you will either print one of those 3 strings or the number.

You should have output like this:

1
2
"fizz"
4
"buzz"

Whereas, your output currently would look like this:

1
2
"fizz"
3
4
"buzz"
5

I know your code is going in descending order but I showed ascending order.

Also, I missed it before but you don't need to do the var keyword twice on the counter variable or initialize it twice.

var counter;

for (counter=100; counter; counter = counter -1) {

You should only print the number if you're not printing one of those strings. You need to add an else block.

Definitely.

You don't need to do the var keyword twice on the counter variable or initialize it twice.

That's an error. JavaScript does not have block scope so you should declare any variables inside of loops.

Iam just wondering why is my while loope stucked in infinite fizz looping ? :)

var number = 1;
while(number <= 100 ) {

    if (number % 3 ==0 & number % 5 ==0) {
        console.log('fizzbuzz');
    } else if (number % 3 == 0) {
        console.log('fizz');
    } else if (number % 5 == 0) {
        console.log('buzz');
    } else {
        console.log(number);
        number ++;
    }

}

Hi Tibor,

I think you're getting 1, 2, and then infinite "fizz"?

What happens when you get to 3 is that you print "fizz" and the number doesn't get incremented because it's inside the else block. Then you're stuck looping and printing "fizz"

You have to move your number increment to the end of the loop outside of the if/else block.

You also should use the logical AND, && and not the bitwise-AND &

I think it works out ok in this case but it's not what you're trying to do.from a program logic standpoint.

Oh thank you very much Jason for the explanation:) I really couldnt figure it out after the first course of js.