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

EXTRA CREDIT fizzbuzz assignment HELP NEEDED (code inside) beginner.

So I have been working on an extra credit assignment for javascript but I can't seem to figure it out.. This is the assignment:

Extra Credit Fizz Buzz

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.

My code so far:

for (var teller=1;teller <101;teller = teller +1){ console.log(teller)

if (teller %3){
} else {
    console.log("fizz");
}

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

}

Problems:

1) It doesn't replace the numbers but instead goes like: 1,2,3,fizz, 4, 5, buzz, etc. (It should be 1,2,fizz, 4, buzz, etc.)

2) how do I code multiple if's? (if the number is a multiple of 3 AND 5)

If been trying to crack it for a couple of hours, but can't find it. Any help?

Thanks.

Jeroen

5 Answers

I recommend using an if/else block, rather than multiple separate if statements. It makes the code easier to read. Here's the flowchart of what you want to do:

  1. If the number is divisible by 3 and 5, print "fizzbuzz".
  2. Else if the number is divisible by 3, print "fizz".
  3. Else if the number is divisible by 5, print "buzz".
  4. Else print the number.
for (var i  = 1; i <= 100; i++)         // i++ is a shorthand way of writing i = i + 1
{
   // This is how you check multiple conditions in a single if statement.  The && means "and".  You can do an "or" by using ||
   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);
   }
}

You could simplify the "divisible by 3 and 5" if statement because if a number is divisible by 3 and by 5, it it also divisible by 15. So you could just check for divisibility by 15 instead of checking 3 and 5. But both ways work.

Another thing; it's better to do if (i % 3 == 0) instead of doing if (i % 3) and putting the log statement in the else block. Both ways will work, but you want to avoid having an if statement with an empty body and only having code in the else block.

Hi Ben, I am working on this assignment too. I would like to know why is it == 0 (false) instead of 1(true) if were to print the word when it meets the condition?

While Ben has the best answer, I am fairly certain that SEVERAL concepts are missing from the source material that will make this example difficult for some to follow. 1) i++ as ( an incriminator as you showed in your comments) 2) the difference between an assigner =, equals ==, and strictly equals === 3) Logical AND && (as you showed in your comments) 4) else if

//Mine is almost identical to Ben's. I just used strictly equal instead of equals. It's just kinda a habit I got into.

for( var i = 1; i <= 100; i++ ){  // i++ as the shorthand incriminator 
    if( i % 3 === 0 ){
        console.log("Fizz");
    }else if( i % 5 === 0 ){
        console.log("Buzz");
    }else if( i % 3 === 0  && i % 5 === 0 ){   // && is logical AND
        console.log("Fizz Buzz");
    }else{
        console.log( i );
    }
}
if (teller %3){
      console.log("fizz");
      break;
}
if (teller %5){
      console.log("buzz");
      break;
}
 console.log(teller);

Break the current loop if teller is %3 or %5 to stop it from printing both the string and the number.

Thanks guys!! much appreciated.

Nicholas - the modulus operator (the % sign) performs a division, but the result is the remainder (instead of the result being the quotient like in regular division). Some examples:

  • 13 % 4 = 1 (13 / 4 = 3 remainder 1)
  • 15 % 3 = 0 (15 / 3 = 5 remainder 0)
  • 27 % 5 = 2 (27 / 5 = 5 remainder 2)
  • 15 % 5 = 0 (15 / 5 = 3 remainder 0)

As you can see, the result of the modulus operator only gives a result of 0 if the first number is evenly divisible by the second number.

The question says you only want to print the words if the number is a multiple of 3 or a multiple of 5. If a number is a multiple of 3, is has to be evenly divisible by 3. If a number is a multiple of 5, it has to be evenly divisible by 5, and so on. The if` statements are not really checking for true or false per se; they're checking to see if the result of the modulus operation is zero or is non-zero.

Thanks so much for the answer. Now I understand what it means already, it's basically checking whether the calculation returns the wanted result instead of true or false answer. Thanks again.

this is a tough question - there was no where near enough prep for this stuff.