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

Hints for those attempting the Fizz Buzz Extra Credit

I got frustrated when checking out the forum earlier, because the help given for Fizz Buzz incorporated code that us beginners have not learned yet!

I completed the assignment using only code that has been explicitly taught to beginners from Treehouse. Here are some HINTS for my fellow beginners:

  • You want to print (i.e. console.log) your "normal numbers" AFTER your fizzbuzz, buzz, and fizz, numbers.
  • don't forget that not only can you use "if" and "else" statements, but also "else if" statements. Use the "else if" like a stepping stone to the next part of your code.
  • new code will not overwrite old code! If a number is already a "fizz" or "buzz" it will not become "fizzbuzz" based on new code
  • One IMPORTANT tidbit that they DID NOT TEACH US is that when you put in the code for your modulator (i.e. if (counter % 3 = 0)) you must use TWO equals signs. Thus it should be written as: if (counter % 3 == 0) (note that I used "counter" as my variable name; you may have used a different name)

Those are the hints! I urge you to figure it out on your own, as it's a valuable learning experience, but for those who are interested, or want to compare their finished work with mine, my code is below:

SPOILERS:

//count from 1 to 100
for (var counter=1; counter<=100; counter=counter+1) 

//print fizzbuzz for every multiple of 3 AND 5
if (counter%15==0) {       
    console.log("FIZZBUZZ");
} 

//print buzz for multiples of 5
else if (counter%5==0) {   
    console.log("BUZZ");
} 

//print fizz for multiples of 3
else if (counter%3==0) {   
    console.log("FIZZ");
} 

//without this code below none of the "normal" numbers are printed
else {                     
    console.log(counter)
} 

Feel free to sed me questions or feedback. :)

3 Answers

Kelly de Vries
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Kelly de Vries
Treehouse Guest Teacher

I also figured out what you have above but in a slightly different form and discovered that the order of my if checks mattered. I originally did my "fizz" and my "buzz" if statements before the "fizzbuzz" and found that I needed to put the least inclusive if statement first. Also, they have not yet taught us the double ampersand, which is how you represent "and" (when you are wanting both to be true).

Here's what my version looks like-

for (var number=1; number<101; number=number+1){ 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); } }

Well done!

  • Yes, your comment about "order of checks" is my hint that new code will not overwrite old code :)
  • You can use && but you don't have to. Since 5 * 3 = 15 you can just tell it to look for multiples of both by looking for multiples of 15 (i.e. if (counter%15==0)), but your method is just as effective, although a bit longer and more cumbersome to read.
  • Thanks for the tip! I hadn't learned the &&. That should come in handy
  • Your console.log "Number = " + number is a nice touch

Thanks so much for giving nudges but not too much! I will say that I had to peek at the "else if" statements to get it—and the other responses had a lot of spoilers w/r/t multiples of 15—but this was really a big help!

Mine didn't use the for, so it's a bit more basic, maybe? I also added a Begin and a Done so I could see if the program was getting to the end.

SPOILERS

console.log("Begin");

var counter = 100;

while (counter) {

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

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

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

    else {
        console.log(counter);    
    }    

    counter = counter -1;
}

console.log("Done");

Great job! I'm glad it was helpful :)

For learning purposes, I wish there were more code challenges, as well as some hints like these to help us along. I should write Treehouse about that sometime--I've been going elsewhere just to find coding exercises to practice on!

Apparently, we can also use this:

while (counter < 100) { 
    if (counter%3==0 && counter%5==0) { 
        document.write('fizzbuzz '); 
    } else if (counter%3==0) { 
        document.write('fizz '); 
    } else if (counter%5==0) { 
        document.write('buzz '); 
    } else { 
        document.write(counter + ' '); 
    } 
    counter = counter + 1; 
}

Tried a few variations. Quite fun!