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

Fizz buzz challenge

Seriously stuck on the fizz buzz challenge on http://teamtreehouse.com/library/programming/introduction-to-programming/control-structures

i have used for (var number = 100; number; number = number - 1){ console.log(number);}

which counts down the variable but when i want to implement the fizz, buzz and fizz buzz i can't seem how to figure this out help me please

The thing that caught me out is that the 'equals to' operator is ==

Extra hints:

you need to use 'else if' statements the statement needs to evaluate as true for it to run think of a number you can divide by that covers numbers which can be divided by both 3 and 5 look at the order in which you call the statements in order for fizzbuzz to print

disclaimer - I'm no expert so sorry if anything is inaccurate.

Actually I have just learnt (through doing) that you can achieve the same result with just a series of if statements and then and else statement at the end; the advantage of which is that it doesn't matter what order you call the statements in and it stil works.

10 Answers

Shaker Advertising
Shaker Advertising
5,395 Points
// Using the && operator will ensure the console.log only runs if BOTH conditions are met.
// If you use the || operator that will run the console message if EITHER condition is met - one OR the other.
}else if(Condition 1 && Condition 2){  
    console.log("FizzBuzz"); 
}
Alex Morask
Alex Morask
6,430 Points

Thanks Connor, I got it to work. For some reason, I had to use the && operator in the first IF statement rather than in one of the ELSE IF statements. I'm not sure why that is, but it's working none-the-less!

Chase Lee
Chase Lee
29,275 Points

Can't figure it out either. Just skip it for now because it's extra credit.

Hello Tunde,

You currently have (re-formatted for easier reading), which does print the numbers 100 to 1 correctly:

for (var number = 100; number; number = number - 1)
{ 
    console.log(number);
}

What you need to add is a set of if, else conditional statements in the loop to change what you write to the console with console.log. I will give you an example to help illustrate it. In my example I want to count down from 10 to 1 and if it is a multiple of 2 then instead write your name, Tunde:

for (var number = 10; number; number = number - 1)
{ 
    if(number % 2 == 0) {
        console.log("Tunde");
    } else {
        console.log(number);
    }
}

I have created a fiddle so that you can view this code running . I hope this helps clarify how to create the solution, if you have any more questions let me know. Good luck!

James Barnett
James Barnett
39,199 Points

Codie Mullins -

Jsfiddle doesn't have a built-in JavaScript console, so I use jsbin instead.

James Barnett
James Barnett
39,199 Points

Tunde Adegoroye -

My suggestion is to come back to the FizzBuzz exercise after you've completed JavaScript Foundations and have some more experience programming.

Scott Prock
Scott Prock
666 Points

LOL ... looks like I too will need to come back to this. I'm brand new to coding. Well, funny thing, I can read most programming and understand what is going on, but never learned the basics of syntax and such to be able to write it.

My code results so far ...

for (var counter = 1; counter < 101; counter = counter +1) {

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

}

This only prints out 33 fizz and no numbers ... looks like I may be close, but I will come back.

Scott Prock
Scott Prock
666 Points

aha ... after pasting the code, I realized where my mistake was. I failed to print the else statement to the console.log. This whole process helps imprint this stuff on my brain, retention isn't what it was 20 years ago HA!

Scott Prock
Scott Prock
666 Points

YEP, will need to come back to this one ... the following code didn't work either ... close, but not what is being asked for.

for (var counter = 1; counter < 101; counter = counter +1) {

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

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

}

Here is what I used and it worked.

code redacted
James Barnett
James Barnett
39,199 Points

Grace Drone - I've removed your code.

Remember we are here to give help, not answers, our goal is to encourage and guide those needing help, and not just give them an answer. Need more explanation on the distinction ... check this out.

On the subject of extra credit in particular, it's meant to stretch students abilities to learn things and try them out on their own. Think of them as brain teasers, the value is in the journey not the destination.

Sorry! Thanks for letting me know.

James Barnett
James Barnett
39,199 Points

If you still want to give help with the fizzbuzz extra credit look at the code posted and give the person some debugging tips.

Shaker Advertising
Shaker Advertising
5,395 Points

So this might be old news but it's something that wasn't covered in the videos: the use of if/else if/else statements.

For example:

if("condition one"){
    console.log("condition one was met");
} else if("condition two"){
    console.log("condition two was met");
} else {
  console.log("remaining content will be output here");
}

Hope this helps without giving away too much.

Alex Morask
Alex Morask
6,430 Points

Hey everyone,

So I've almost got this down. I've got numbers divisible by 3 changing to "Fizz" and numbers visible by 5 changing to "Buzz", but I'm struggling with the last part where you need to change any number divisible by both 3 AND 5 to "Fizzbuzz". It's probably a little farther ahead, but is this where we're supposed to be utilizing either the || or the && operators in the Else If statement?

I had something along the lines of:

else if (Condition 1 || Condition 2) {
console.log("FizzBuzz")
}

Any and all help is appreciated! Used Condition 1 and two because I didn't want to give anything away. (First time forum poster too so my apologies if we're not supposed to post more questions in the Answer sections).

Jamie Harper
Jamie Harper
1,734 Points

Hi Alex - I hope you found a solution to your issue already (4 months is a long time to wait!) but I found through other courses that starting with the most complex/least likely outcome first in the loop is normally the best practice.

Like;

If (I win the lottery & it's a Tuesday & it's snowing & I'm wearing odd socks) { console.log('Holy moly how did this all happen in one go'); } else if (I win the lottery & it's a Tuesday) { console.log('holy moly I won the lottery on a Tuesday??!'); } else (it's a Tuesday) { console.log('ok well it's Tuesday'); }

Notice how in all circumstances, it's a Tuesday - but by having the first one be the most complex - I get to think about it on its own first. It also takes those out of the equation (so to speak). It's like a filtration process; start with the biggest rocks; take them out first. Then filter the next size down, and so on.

Adam Menczykowski
Adam Menczykowski
2,319 Points

Thanks for the tips guys!

I realise that I cannot add the solution in here, but I will say that these things really helped:

To pass two conditionals (if divisible by 3 and 5) use the && operator.

Another tip, instead of outputting fizz / buzz / fizzbuzz, I outputted the counter's value followed by " is divisible by 3" etc to debug the code as I was writing, enabling me to understand the output better.

I also started with an if statement, then a series of else if statements, followed by an else for all other results that didn't match the above criteria.

Can anyone chime in on how to avoid 'ordering' if, else if and else statements to get the same result, thus allowing flexibility for more complex declarations?

Jamie - Thanks for your looping best practice tip! It was very helpful and I'm sure I'll use it as I continue to take more courses.