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 in JavaScript

Hi! I'm having trouble doing this Extra Credit in JavaScript: 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.

I have the following code, but it is not really corect:

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

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);
        }
    };
};

};

Some help, please?

2 Answers

Paul Johnson
Paul Johnson
18,924 Points

Your for loop syntax is slightly wrong, as is your if/else syntax

Your for loop should look like this

for(var i = 100; i >= 0 ; i -= 1)

And your if/else statements should look like this

if (condition) {
   //Do something
} else if (condition) {
   //Do something
} else {
   //Do something
}

Hope that helps

Thanks, but how to add an if condition that checks if i is multiple of 3 and 5 in the same time?

Paul Johnson
Paul Johnson
18,924 Points

Sorry, I did notice you had missed that, just forgot to put it in my original post!

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

Thank you very much for your help!

Write your code putting >>.#.<< the phone thingy. Idont know the answer anyways but it gonna be easyer for one that will help you to read it and it will save time later one . Use this at the beggining of your code >.#.< that linear mark or whatever it is named.