Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Introduction to Programming Control Structures Loops

fizzbuzz extra credit

I got this to run almost perfectly with the code below. When it runs, the if value in the center does not replace the divided value; the name prints above the number. I tried commenting out one of the values and it ran perfectly with any two values. I have also tried changing the order of the if values to no avail. Why is it running perfectly with 2 values but not with three values?

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

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

if (number % 3 == 0) {
    console.log("fizz");
}

if (number % 5 == 0) {
    console.log("buzz");
}

else {
    console.log(number);
}

}

Thanks so much for any help someone can give me on this.

11 Answers

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Sharon,

When you want to execute multiple conditions, use else if.

for (var number = 100; number; number = number - 1) {
    if (number % 15 == 0) {
        console.log("fizzbuzz");
    }

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

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

    else {
        console.log(number);
    }
}

This way, if one condition is false, it will move on to the next one. I'm pretty sure that because of the way you had your code set up, multiple if blocks were being run, instead of one block with multiple conditions. That's why you were seeing both multiples of 3 as well as the word "fizz."

May I post a follow-up question?

I tried to get it counting up from 1 to 100 ... (had to come here to understand the "mod 15" thingy)

What I wonder is: How to get the console.log-output started at 1, instead of 0?

Because even if I write

for (var counter =1; ...

... the console still starts with 0, putting out a "fizzbuzz" ...

Here's what I wrote:

for (var counter = 0; counter < 101; counter = counter +1) {
//mod15: check if the number can be divided by 3 AND 5
        if (counter % 15 == 0) {
            console.log("fizzbuzz", counter);
        }
        //check if the number can be divided by 3
        else if (counter % 3 == 0) {
            console.log("fizz", counter);
        }
        //check if the number can be divided by 5
        else if (counter % 5 == 0) {
            console.log("buzz", counter);
        } 
        //put out numbers not divideable by 3 OR 5
else {
            console.log(counter);
        };
};

... any hint is appreciated ;) Thanks

Thanks Jason - tried it again today & all works fine with var counter = 1 ... actually my comment is unnecessary then. can I just delete it?

anotherdude,

I tried your code but changed it to var counter = 1 and it started at 1 as expected.

Why is this extra credit question presented before the instructor has covered the code necessary to complete it? Or am I missing something?

Thank you, thank, thank you!! That was so simple, wish the instructor had mentioned 'else if' in the video.

Steven Byington
Steven Byington
13,584 Points

Another way to write out the code for the same effect would be wrapping an if/else statement inside another if statement. Here is my code:

for (var n=1; n<=100; n++) {

    if (n%5 === 0){     
        if (n%3 ===0) {
            console.log("fizzbuzz");
        }
        else {
            console.log("buzz");
        }
    }

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

    else {
        console.log(n);
        }

}   
Hung Pham Viet
Hung Pham Viet
9,104 Points

Here is mine using WHILE loop:

var number = 100;

while (number) {
    if (number % 15 == 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 = number - 1;
}

Thank you so much for taking the time to explain all this. I really appreciate it. Regarding the order of the numbers 3 and 5 that occurred when I was changing the order to see if it would make any difference. I forgot to change it back before I copy and pasted it into to my message. I did correct the order before I received Keven's response so I was able to run my code successfully; although as someone else later pointed out I forgot change fizz and buzz to correspond with the correct number. Have a great day!

Just adding my version of FizzBuzz into the conversation :)

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

Couple of things I've used that I found outside of "class":

  1. i++ is shorthand for i = i + 1
  2. && is the logical operand, AND. Both expressions need to evaluate as true for the condition to be true.

Sorry, playing around and came up with an alternative version which consists of solely if statements (no if else statements):

for (var number = 1; number <= 100; number++) {

  var output = "";

  if (number%3 === 0) {
    output = output + "Fizz";
  }

  if (number%5 === 0) {
    output = output + "Buzz";
  }

  if (!output) {
    output = number;
  }

  console.log(output);

}

This version starts with an empty string (output) then uses concatenation to add "Fizz" or "Buzz" to the string. It doesn't need another if statement to check for "FizzBuzz"... that case has already been covered.

Finally, if the output string is still empty (i.e. it's a falsy), the current number is assigned to output.

for (var count= 100; count> 0; count--) { if (count% 15 == 0) { console.log("fizzbuzz"); }

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

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

else {
    console.log(count);
}

}

Vince Usher
Vince Usher
7,159 Points
for(i=0;i<100;)console.log((++i%3?'':'Fizz')+(i%5?'':'Buzz')||i)
for (var number = 100; number > 0; number--) {
    if (number % 15 == 0) {
        console.log("fizzbuzz");
    }

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

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

    else {
        console.log(number);
    }
}

Had to fix up Kevin's code. This should work properly. When your loop finds a number divisible by 15. As soon as it hits the first 'if' block, and the condition is true, your console will print out "fizzbuzz" and it will break out of the 'if else/if ' block all together and continue with the loop. It will ignore everything subsequent of the 'if else/if' block as it should.

If the first condition is false, it checks the next condition which is if the number is divisible by '5', if that is true it will break out of the 'if' block and log "fizz" out to the console... this goes for all cases of an "if, else/if" statement.

If you had a made a conditional check of '3' before you made a conditional check of '5', when the condition of '3' is true, it will print "buzz" and break out of the if else/if block and it will never check that last condition of '5'.

Order of execution is crucial as this is a procedural programming paradigm.

Hi Andrew,

What was wrong with Kevin's code? I don't see anything wrong with it.

Re: order of execution

The mod 15 check does need to go first but it doesn't matter which order you put the mod 3 and mod 5 check. The program should run slightly faster though if you put the mod 3 check before the mod 5 check.

The problem does require that you output "fizz" for multiples of 3 and you're having it output "buzz" instead. You have your "fizz" and "buzz" reversed.