How can I set a default return with if/else if/else statements like I do with the default case in a switch statement?

Question

How can I set a default return with if/else if/else statements like I do with the default case in a switch statement?

Answer

We can set a default condition when using if/else if/else statements, like we do with the default case in a switch statement, by simply using an else statement after the rest of our if and else if conditions.

Example:

let x = 2;

if (x >= 10){
  //do something
} else if (x <= 1){
  //do something else
} else {
  console.log(`x is ${x}`);
}

The else statement acts as a catch-all for when our if or else if statements do not evaluate to true.

1 Like

This is how I solved this challenge!

const tipCalculator = (quality,total) => {
switch (quality) {
  case 'bad':
  return (5/total)*total;
  case 'ok':
  return (15/total)*total;
  case'good':
  return (20/total)*total;
  case 'excellent': 
  return (30/total)*total;
  default:
  return (18/total)*total;
  }
};

1 Like

Just two things I’d like to point out.

  1. To make your code a bit more readable, you should indent your return statements under the corresponding case. Your switch block should also be indented inside the function.

  2. All of your return statements return (num / total) * total. This simply returns num. What could you change so that it returns the correct tip amount?

Example on (num / total) * total.
(5 * 3) / 3 = 5
(a * b) / b = a
2 Likes

It is so easy to solve this task :
as we know, the total is always 100% so we want to solve this task, we should solve it like so :

const tipCalculator= function (quality, total) {
  switch (quality) {
  case 'bad':
  return (5*total)/100;
  case 'ok':
  return (15*total)/100;
  case 'good':
  return (20*total)/100;
  case 'excellent': 
  return (30*total)/100;
  default:
  return (18*total)/100;
  }
};

well, I was going to post my code, but I see myCode === yourCode so I guess I am late to the party…
anyway happy coding.

4 Likes

Hi, I’d just like to contribute my code:

const tipCalculator = (quality, cost) => {
  let tip = 0;
  switch (quality) {
    case 'bad' : tip = 0.05; break;
    case 'ok' : tip = 0.15; break;
    case 'good' : tip = 0.2; break;
    case 'excellent' : tip = 0.3; break;
    default: tip = 0.18;
  }
  return Math.round(tip*cost)
};

2 Likes

hi, can anyone help me out with why this isnt working please?

const tipCalculator = (quality, total) => {

if (quality = ‘bad’){

return (5*total) / total;

}if (quality = ‘ok’) {

return (15*total) / total;

}if (quality = ‘good’){

return (20*total) / total;

} if (quality = ‘excellent’){

return (30*total) / total;

}defult: {

return (18*total) / total;

}

}

1 Like

Just Like to say a big thanks to all who has helped me along the way with these challenges.

love looking at the different codes that you all come up with, so here’s mine:

const tipCalculator = (quality, total) => {

switch (quality){

case ‘bad’:

return 5/100;

break;

case ‘ok’:

return 15/100;

break;

case ‘good’:

return 20/100

break;

case ‘excellent’:

return 30/100

break;

default:

return 18/100

break;

}

};

console.log (tipCalculator (‘ok’, 100));

This also passes:

const tipCalculator = (service, totalCost) => 
service === 'bad' ? totalCost * .05:
service === 'ok' ? totalCost * .15: 
service === 'good' ? totalCost * .20:
service === 'excellent' ? totalCost * .30 : totalCost * .18;
1 Like

I am working on a if/if else/ else statement if you are still stuck on this. hold tight

My code so far is as follows

// Write your function here:
const tipCalculator = (quality,total) => {
if (quality === ‘bad’) {return total * 0.05}
else if (quality === ‘ok’) {return total * 0.15}
else if (quality === ‘good’) {return total * 0.20}
else if (quality === ‘excellent’) {return total * 0.30}
else {return total * 0.18}
}

console.log(tipCalculator(‘good’, 100))

// Uncomment the line below when you’re ready to try out your function
// console.log(tipCalculator(‘good’, 100)) //should return 20

// We encourage you to add more function calls of your own to test your code!

the difference once I looked at the forums, since mines was similar to yours was the 3 equal signs!
They make the world of difference, otherwise, it will get stuck on the first part of code

1 Like

In the code you posted, you are not comparing if the quality argument given is equal to ‘bad’, ‘ok’, ‘good’, etc.; you are assigning that value to the quality parameter.

Remember that the assign operator is “=”. It’s used to assign a value so, if you write quality = ‘bad’, you’re saying to the machine that quality, now, has a value of ‘bad’.

To compare, you can use either the equal operator == or the strictly equal operator === (this last one is usually the most recommended as compares both the value and the type).

I would also suggest you use the if / else if / else statement instead of separate if statements. Basically, you write if (first condition) {}, then you start using else if (condition) {} for the rest of the conditions. Finally, you can add an else {} to put a default value like in the exercecise or to cath an error, e.g. you can write else {return “Error: please, value the quality of the service with bad, ok, good or excelent”}. See the example:

const tipCalculator = (quality, total) => {
    if (quality === 'bad') {

      return total * 0.05;

    } else if (quality === 'ok') {

      return total * 0.15;

    } else if (quality === 'good') {

      return total * 0.2;

    } else if (quality === 'excelent') {

      return total * 0.3;

    } else {

      return total - 0.18;
    }
}

I will paste the code I used for the exercise. I used the swjtch statement:

const tipCalculator = (quality, total) => {

   switch (quality) {

       case 'bad':

           return total * .05;

       case 'ok':

           return total * .15;

       case 'good':

           return total * .20;

       case 'excellent':

           return total * .30;

       default:

           return total * .18;

   }

}
3 Likes

I solved it the same way, but I think your mathematical statement is faulty. It only works if the total is 100. If the total would be 50 for example. You would get (5/50)50 = 0.150 = 5. 5 is 10% of 50 and not 5%.

return total * 0.05 is more accurate! c:

I think the best way to solve the tipCalculator() challenge is this way:

const tipCalculator = (quality, totalCost) => {

switch (quality) {

case 'bad':

return .05 * totalCost;

case 'ok':

return .15 * totalCost;

case 'good':

return .20 * totalCost;

case 'excellent':

return .30 * totalCost;

default:

return .18 * totalCost;

}

}

console.log(tipCalculator(‘good’, 100)) //should return 20

1 Like

it was 0.05*total, or be error.

let tipCalculator = (rating,amount) => {

switch (rating) {

case ‘bad’:

return amount*(5/100);

break;

case ‘ok’:

return amount*(15/100);

break;

case 'good':

return amount*(20/100);

break;

case ‘excellent’:

return amount*(30/100);

break;

default:

return amount*(18/100);

}

console.log(tipCalculator('bad', 100))

};

console.log(tipCalculator(‘bad’, 100))

I have chosen to write an if/else control flow for this exercise, however apparently I have a syntax error somewhere. Can someone please help me with this?!

function tipCalculator(quality, total)
{if quality === ‘bad’
{return total * .5}
else if quality === ‘ok’
{return total * .15}
else if quality === ‘good’
{return total * .20}
else if quality === ‘excellent’
{return total * .30}
else {return total * .18}
}
console.log(tipCalculator(‘good’, 100)

I think the ( ) stuff is missing.
if quality === 'bad'
should be
if (quality === 'bad')
and so on …

1 Like

Yup! That was it. Thank you!

Here is my solution that worked fine !

const tipCalculator = (quality, total) => { if (quality === 'bad') { return (total * (5 / 100)); } else if (quality === 'ok') { return (total * (15 / 100)); } else if (quality === 'good') { return (total * (20 / 100)); } else if (quality === 'excellent') { return (total * (30 / 100)); } else { return (total * (18 / 100)); } } console.log(tipCalculator('bad', 100)) //

It only works if the total is 100. If the total would be 50 for example. You would get (5/50) * 50 = 0.1 * 50 = 5. 5 is 10% of 50 and not 5%.

return total * 0.05 is more accurate!

1 Like