Intro to Objects: FIZZ BUZZ please can you help?


#1

Hi I have sorted the code but I have problem in understanding one thing.
Please help me to understand if you know why?...

... When I write code like this everything is OK (code below)

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

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

    else {console.log(i);
    }

THAT WAY IT WORKS BUT

Why it doesn't work if I write it like this

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

In first instance my code is ok (Fizz, Buzz, FizzBuzz are displayed) however if i place && equation as the last one my code prints out only fizz and Buzz ignoring FizzBuzz. I am curious why? Is it because one condition has been met? Therefore computer doesn't seem to be acknowledging the && statement further down the code?

If someone knows I would be grateful for a explanation.
Thank you in advance.
Matthew from Poland :wink:


#2

Hi!, first you need to use <= on loops I think its mandatory but I could be wrong!... now for the second question, the code is the same but! with your second code.

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

Use number 15 as example, 15 % 3 = 0 // TRUE! and also 15 % 5 = 0 // TRUE! so you should print fizz buzz BUT because you evaluate 3 first and the condition is meet your code will never make it to the 3 || 5 part :smiley:
Hope it helps


#3

Hi codesolver20899

Thank you very much for the explanation.

You are my hero :wink:


#4

It worked only when i tested first 15, 5, 3..

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


#5

I'm having an issue with my loop on this project. Error says I'm printing wrong numbers but my loop is correct???
function numbers(){
for(var i = 1; i <= 20; ++i){
console.log(i);
if(i % 3 == 0 && i % 5 == 0){
console.log(i + " " + "FizzBuzz");
}
else if(i % 5 == 0){
console.log(i + " " + "Fizz");
}
else if(i % 3 == 0){
console.log(i + " " + "Buzz");
}
else {
console.log(i);
}
}
};

A second pair of eyes would be highly appreciated


#6

Ugh...Nevermind

I'm used to normal coding practices and this project requires you to follow the paradigm precisely. I removed the outside function and removed my string concats. Works just fine.

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