...And the good!


#1

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

#3

But then when you get to the number which is divisble by 3 and 5 it will print fizz, buzz, fizzbuzz - so to counter that I added a bit more like so...

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

However, mine still gets an error..

Oops, try again. It looks like you printed out the wrong number of items.

Where am I going wrong?


#5

I have fixed it... the instructions say to print out the other numbers if not divisble by 3 or 5 so i nested it all in another if/ else statement to check for that first... problem solved and passed! :slight_smile: complete code below..

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

#6

Thanks for the help Bandit. But actually, I was posting a successful code that does exactly what's required. (I maybe didn't need to include var answer = "" but I like to initialise variables) It's just for some reason the bit where I said it was successful doesn't seem to have posted. Must have accidentally deleted it before I hit the post button.

You have me wondering about the semicolons after the brackets though. They are individual IF statements so shouldn't they have semicolon's after them? I come from a language that doesn't use end-of-line identifiers and I've noticed through these lessons that sometimes lines don't need the semicolons and I can't work out the pattern.

The second IF uses += so that Buzz gets added to Fizz for any values that are divisible by both 3 and 5. This is why "answer" is only logged to the console at the end. The third IF only executes if the number is divisible by neither. This way all conditions are covered.


#7

Thats great we both have different correct answers to the same problem, I love how in js there are different ways to do things.. but can we find out which one is the best or more efficient.. which one is best practice does anyone know?


#8

kade.no1's solution is shorter and gets the job done, which is usually preferable. Yours works exactly the same though and is only slightly larger. The "best practice" is to keep your code as short as possible to make it easier for others to read.

If you're comfortable with the way you're doing it, then as long as it works, keep doing it. They're both great ways to code, again, only as long as they work.

Good work to both of you! :+1:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.