...And the good! 3/33 Help!


#1

Can someone help me with this game? I'm getting error message: "Oops, try again. It looks like you printed out the wrong number of items." What am I missing?

for (var i =1; i < 21; i++){
console.log(i);
};

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


I can't find why it prints out Fizz when it should print 1 please help!
FizzBuzz Exercise
#2

You should first check if both conditions are true.

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

#3

I feel silly. It didn't occur to me that you could have more than one else if follow the way you did here. I kept referring back to previous usages thanks for the help here


#4

It's ok, it's part of the learning process. Keep in mind that you should always check the most specific cases first, that's why we first check if counter is divisible by both number, and then if it's divisible by one number only.

And to refer to the part that you didn't know you can have more else if parts. You can actually split this into 4 if loops if you want, but if ... else if ... else if ... else look better and saves time.


#5

Hi igorcaletacar,

Does the order in which you check conditions matter? you mentioned in your solution checking whether the conditions if i divided by 3 and 5 first and then checking the others. Can you explain why?
Thanks in advance


#6

Sure.
You know how if works, right? If something is true, do something. If not, do something else specified in else block.

Now when you have if ... else if ... it is crucial you check first for most specific condition. If loop is evaluated by one at time. Meaning that if first if is false, code goes on to else if. If first if is true, everything else is skipped.
Here is an example to show you what I mean.
This is traditional if else if loop.

// traditional way
if (condition1) {
   // statement1
} else if (condition2) {
   // statement2
} else if (condition3) {
   // statement3
} else {
   // statement4
}

Here is the same loop written in if else form

if (condition1) {
   // statement1
} else {
   if (condition2) {
      // statement2
   } else {
      if (condition3) {
         // statement 3
      } else {
         // statement 4
      }
   }
}

So you see, if condition is first loop is true, noting else is checked. If it's not, it goes to else part. There it checks second condition. If it's true, it stops. If not, it continues.

Let me know if this needs more explaining.


#7

Good afternoon Igor,

I feel there is something fundamentally important here that I am not picking up. Could you please explain again using a practical example and showing why one method is more efficient and proper than the other method? I 'think' I understand what you are saying, but I am not certain.

Before I found this post, I did what you said and created the most specific condition first, which for some reason felt natural to me, however as you explain it, there is a fundamental benefit to doing so. I would like to understand it better.

Thank you for your time!

David


#8

Hi David, I am very busy lately so I missed your question.
Let me check if I got it right. You just want me to give an example (that could actually happen in real life)? If so, I will try to keep it as simple as possible.

So lets say you need to make a function that will take some number and if number is smaller than 5, it will return it's square value. but if the number's value is 4, you don't want to return it's square value, but multiply it by 2 instead. If number is bigger than 5, we just return the number. Can you see where this is going?

If we were to organize our code like this:

// EXAMPLE 1
// some code
if (number < 5) {
 return number * number;
} else if (number === 4) {
 return number * 2;
} else { 
 return number;
}
// some code

Now this would actually never trigger our `number === 4` condition, and we would be missing one most specific case. Our program would still work, and it would work for infinite - 1 number of situations, because ˙we will never check if equals 4 before doing some other operation. From this it should be obvious that you should always take care of most specific situations first, and more loose later. Example of how this should have been done:

// EXAMPLE 2
// some code
if (number === 4) {
 return number * 2;
} else if (number < 5) {
 return number * number;
} else { 
 return number;
}
// some code

We could put even more conditions there. Let say we have to do one action when number equals our specific condition, one when number is in some small range and another when number is in some big range.

// EXAMPLE 3
// some code
if (number === 4) {
 return number * 2;
} else if (number > 0 && number < 5) {
 return number * 3;
} else if (number < 10) {
 return number * number;
} else { 
 return number;
}
// some code

I put it like this so you can see that out most specific condition is in our second conditions range. I could have written it like this:

// EXAMPLE 4
// some code
if (number > 0 && number < 5) {
  if (number === 4) {
    return number * 2;
  } else {
    return number * 3;
  }
} else if (number < 10) {
  return number * number;
} else { 
  return number;
}
// some code

Notice that in example 4 we would determine that number = 4 is in first conditions range, and then check if it's value is equal to 4. In example 3 we would check if number equals to 4 first, and then move on. Now I would say that if you have big number of if's, maybe it would be smarter to use example 4 code since it's better organized I'd say. Otherwise, example 3 all the way.

Now back to example 3. If our number equals to 4, we check it and finish loop. If it equals, lets say 3, we evaluate false in first loop and true in second and finish. If it's -3, we evaluate it as true in third loop and then finish.

I probably said a lot of things here, but I've tried to keep it as simple as possible and with few examples. If you need more explaining, no problem, give me some of examples that are troubling you and I'll try to help explain them, or make another example on my own.


#9

Good evening Igor,

I apologize for the delay and really appreciate your clear and detailed response. This concept makes a lot of sense to me now. I cannot thank you enough.

Best regards,

David


#10

I'm glad I could help you. If you need more help on this or other topics, feel free to contact me via message, I'll help whenever I can.


#11

If you don't want to check both condition in first 'if' you can try this one. Then you will print numbers that are divisible by 3 and not divisible by 5 (and vice versa).

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

}