3/33 - Heads up!


#1

Just a quick heads up in case you make the same mistake as mine:

I was printing the "Number" + "Fizz" , "Buzz" or "FizzBuzz". Although this logic is correct, Codeacademy was not liking it. You need to print only "Fizz", "Buzz" or "FizzBuzz".depending if you find a number divisible by 3, 5 or 3 and 5.

See code below with comments in case the summary above does not make sense.

for (var numbers = 1; numbers < 21; numbers++) {
if (numbers % 3 !== 0 && numbers % 5 !== 0) {
console.log(numbers);
} else if (numbers % 3 === 0 && numbers % 5 === 0) {
// console.log(numbers + " FizzBuzz"); <----- This line was not necessary
console.log("FizzBuzz");
} else {
if (numbers % 3 === 0) {
// console.log(numbers + " Fizz"); <----- This line was not necessary
console.log("Fizz");
} else if (numbers % 5 === 0) {
// console.log(numbers + " Buzz"); <----- This line was not necessary
console.log("Buzz");
}
}
}


#2

nice. i thought that the console will get confused if we input something like 15, as it will satisfy both (numbers % 3 === 0) and if (numbers % 3 === 0 && numbers % 5 === 0) conditions, so i changed if(i%3===0) to if(i%3===0 && i%5!==0).

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

#3

Thanks avil172 great help !!! else if () 15===0


#4

Forgive me... I do not get why you are "else if(i%15===0)". The number should be divisible by 3 and 5 and not 15. It will probably let you finish the exercise but it does not follow the requirement. Or maybe I am missing something.....


#5

Correct. Should look like:

else if (i % 3 === 0 && i % 5 === 0){
    console.log ("FizzBuzz");
}

#6

I made it this way.

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


#7

LCM => Least Common Multiple

If a number is divisible by the LCM, it is divisible by all its factors. Put another way, if a number is divisible by a given set of numbers, then it is also divisible by their product.

Numbers divisible by 15

15, 30, 45, 60, 75, ...n*15

All of those numbers are divisible by 3 and by 5 since 3 and 5 are factors of 15. 15 is the LCM of 3 and 5.

So if the instructions ask us to find numbers that are divisible by 3 and 5, we are not going against any norm if we choose to use the LCM. It's a logical choice.

if (i % 15 === 0) {
    console.log("FizzBuzz");
} else
 if (i % 3 === 0) {
    console.log("Fizz");
} else
 if (i % 5 === 0) {
    console.log("Buzz");
} else {
    console.log(i);
}

is perfectly legitimate. It is logic application like this that saves on code. Even in a simple setting with simple numbers we can still find ways to apply mathematical logic


#9

yea it worked well.But do you know that if you reserve the order of the condition like-

for( var i=1;i<21;i++){

if(i % 3 ===0 ){
console.log("Fizz");

}else if (i % 5 ===0){
    console.log("Buzz");

}else if (i % 3 ===0 && i % 5===0){
    console.log("FizzBuzz");

}else{ console.log(i);}

}

number 15 will be recorded as "Fizz" because number 15 meets all three conditions and javascript runs the top condition first,then the next,


#10

In an else if branch, only one branch can ever be followed, thus, when i is 15 it follows the first branch. The rest of the branches are not seen in that pass.


#12

You have to put the "FizzBuzz" if statement at the very front.

Like:

if(__% === __ && __% === __){
console.log("FizzBuzz"); //this must be the first statement in your if/else if/else if/else statement.


#13

Yeah, I just figured that out. That's why I withdrew my post. Thx anyway! :grin: