I'm stuck :(


#1

this is my code:

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

#2

re-order your if and else if statements,

if it finds a number is divisible by 3 why would it bother to check if its also divisible by 5? Having that last else if there will never be run as any number divisible by 3 and 5 will be given either fizz or buzz.

Move

somewhere else, let me know if you can't figure it out


#3

put in the first line like so:

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

#4

Don't really like the use of your var no as that can be seen as very confusing.

compare your code to mine, and work out the differences.

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

#5

I think if you will input i < 21 then your last loop will intake 20 + 1 which overexceed the iteration limit


#6

doesn't matter

var i = 1;
for(i = 1; i < 20; i++) {
    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"); 
    }
    console.log(i);
}

#7

No, as you start at 1 it adds 20 on to reach 21.


#8

oh .. you are right but still can't..

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

#9

Main differnces between us, I have an else statement, in my first if statement after && I have i % 5 you have no & 5

why did you change my code here

to i & 5 ? it should be % otherwise its saying i and 5 === 0

its i % 5 because you are checking if the number is also divisible by 5 with 0 left over.


#10

Thx, I didn't see my mistakes on code running. It works with

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

#11

But still confusing about using alternative

var i = 1;
for(i = 1; i < 21; i++) {
    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"); 
    }
    console.log(i);
}

or

var i = 1;
for(i = 1; i < 21; i++) {
    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"); 
    }
}
console.log(i);

In this last case it will print out also "21" pretty awkward!


#12

} else {
console.log(i);
}

will stop it from counting to 21