3

I'm trying to solve a problem for a course I'm currently doing, but I've been stuck on why my code returns undefined for about an hour now.

I've switched a few things around, added brackets to make sure things are in order, idk what else to try.

var bills = [124, 48, 268];

function tipsCalculator(bill) {
  switch (bill) {
    case bill < 50:
      return (bill * (20 / 100));

    case bill >= 50 && bill <= 200:
      return (bill * (15 / 100));

    case bill > 200:
      return (bill * (10 / 100));
  }
}

var testing = tipsCalculator(bills[0]);
console.log(testing);

I expect it to return the calculation of the 124 * whichever case that value fits.

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 2
    you have to use if(..) {...} else if(..) {...} else {...} because switch-case uses constants in predicate – rockfarkas May 13 '19 at 04:58
  • 1
    You can't use comparison on [switch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) like `case bill > 200`. Use `if-else-if` instead. – Shidersz May 13 '19 at 04:59
  • 1
    Also see https://stackoverflow.com/questions/6665997/switch-statement-for-greater-than-less-than – Herohtar May 13 '19 at 05:11
  • 1
    You can accept one answer (if it helps you) by click on big gray check button on its left side. If you wish you can add +10 points to any author of any good answer by click upper gray triangle – Kamil Kiełczewski May 13 '19 at 06:03

3 Answers3

6

That's not a valid use of switch. You will have to use if statements:

var bills = [124, 48, 268];

function tipsCalculator(bill) {
  if (bill < 50)
    return (bill * (20 / 100));

  if (bill >= 50 && bill <= 200)
    return (bill * (15 / 100));

  if (bill > 200)
    return (bill * (10 / 100));
}

var testing = tipsCalculator(bills[0]);
console.log(testing);

The reason it doesn't work is because expressions like bill < 50 evaluate to a boolean value -- either true or false. So when your code executes tipsCalculator(bills[0]) the function looks like this:

function tipsCalculator(124) { // bills[0] == 124
  switch (124) {
    case false: // 124 > 50 == false
      return (124 * (20 / 100));

    case true: // 124 >= 50 == true, 124 <= 200 == true, thus true && true == true
      return (124 * (15 / 100));

    case false: // 124 > 200 == false
      return (124 * (10 / 100));
  }
}

As you can see, the only cases are true and false, but 124 is neither of those, so your function completes without entering any of the cases and since no return statements are executed, the return value is undefined.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
  • So in which case would I use a switch statement? Thank you by the way, I was trying to avoid using if else statements because I knew how to do those. – Rojay Arinze May 13 '19 at 05:01
  • 1
    Basically it's for situations where you need to take a certain action based on a single value -- for example, you could do `case 50:` and it would execute the following code if `bill` was exactly 50. Take a look at the linked documentation for more examples of how it works. – Herohtar May 13 '19 at 05:05
  • 1
    Expression's can be used with switch, although I still prefer if-else – Satpal May 13 '19 at 05:08
  • 1
    @Satpal Not in exactly the sense one would expect though, and I'm not sure it's a great pattern. – Herohtar May 13 '19 at 05:09
  • 1
    @RojayArinze I updated the answer with some more details about why it doesn't work. – Herohtar May 13 '19 at 05:38
3

You use switch in wrong way, you can use it as follows

var bills = [124, 48, 268];

function tipsCalculator(bill) {
  let b = bill < 50 ? 1 : (bill <= 200 ? 2 : 3)
  switch (b) {
    case 1:
      return (bill * (20 / 100));
    case 2:
      return (bill * (15 / 100));
    case 3:
      return (bill * (10 / 100));
  }
}

var testing = tipsCalculator(bills[0]);
console.log(testing);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
  • That might work for this situation, but in general that makes the code kind of confusing to maintain if you need to change the cases later. – Herohtar May 13 '19 at 05:22
2

Try passing true in switch case.

var bills = [124, 48, 268];

function tipsCalculator(bill) {
  switch (true) {
    case (bill < 50):
      return (bill * (20 / 100));

    case (bill >= 50 && bill <= 200):
      return (bill * (15 / 100));

    case (bill > 200):
      return (bill * (10 / 100));

  }
}

var testing = tipsCalculator(bills[0]);
console.log(testing);
Syed mohamed aladeen
  • 6,507
  • 4
  • 32
  • 59