Difference between logic of if-else and if-else-if

Tell us what’s happening:

Following is one of exercise which I did but I was having issue in my if else statement which I had to check with ‘YT video’ to check what is I m missing, I was using if else if whereas in the video the person used if else, I tried to micmic same if else but couldn’t.

if(count > 1){
  return count +" Bet";
}else if(count < 1){
  return count +" Hold";
}

In words, I m saying if count is > 1 then return … else if count is less than 1 do this…
the correct one is

if(count > 1){
  return count +" Bet";
}else{
  return count +" Hold";
}

In words, it says if count is greater than 1 return … else return … (which also means IMO that if it is less than 1)

My question is, isn’t that same thing as else if I wrote above or I am missing something?

Your code so far

let count = 0;

function cc(card) {
  // Only change code below this line
// if(card => 2 || card <= 6){
//   count +=1;
// }
// console.log(count + " Hold");
//   return count + " Hold";
switch(card){
  case 2: 
  case 3:
  case 4:
  case 5:
  case 6:
    count ++;
    break;
  case 7:
  case 8:
  case 9:
    count == 0;
    break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
    count -=1
    break;
}
if(count > 1){
  return count +" Bet";
}else{
  return count +" Hold";
}

  // Only change code above this line
}

cc(2); cc(3); cc(7); cc('K'); cc('A');

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36

Challenge Information:

Basic JavaScript - Counting Cards

return count +" Bet";
}else if(count < 1){
  return count +" Hold";
}

You didn’t include the if statement so I don’t know exactly what your code is. If your if statement is:

if(count > 1){

Then what will happen if count is 1?

1 Like

sorry my bad :grimacing: , if remains the same if (count > 1)

Not exactly the same. Your if else statement and if else if (nested if) statement is just slightly different.

If count is 1 or less than 1, this code will return count + " Hold"
If it’s 2 or bigger, this code will return count +" Bet"

If count is less than 1, this code will return count + " Hold"
If it’s 2 or bigger, this code will return count +" Bet"
Now what if count is 1? Well it won’t return anything, since the code didn’t handle it.

You could use less than or equal (<=) or greater than or equal (>=) to handle this kind of things.

You didn’t answer this question :slightly_smiling_face:

if the count is greater than, it will return Bet, otherwise e.g. if count == 1 it will return Hold

Show me in your code where this happens.

ur right, it does coz it has not value, I got

else if(count < 1){
  return count +" Hold";
}

this should be count <= ?

I’m not going to tell you the answer. Now that you know what the problem is, it is up to you to fix it :slight_smile:

1 Like

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