Wonkook Lee
Wonkook Lee

Reputation: 37

JavaScript, I can not understand switch parameter

I recently started to study javascript I'm currently watching Javascript course in Udemy. While code challenging, There's something I cant get it about parameter of 'switch'

let john = {
  fullName: 'John Smith',
  bills: [124, 48, 268, 180, 42],
  calcTips: function() {
    this.tips = [];
    this.finalValues = [];
    for (let i = 0; i < this.bills.length; i++) {
      let percentage;
      let bill = this.bills[i]

      switch (bill) { // If I put parameter as 'bill' variation, The result is only defalut.
        case bill < 50:
          percentage = 0.2;
          break;
        case bill >= 50 && bill < 200:
          percentage = 0.15;
          break;
        default:
          percentage = 0.1;
      }
      
      this.tips[i] = bill * percentage;
      this.finalValues[i] = bill + bill * percentage;
    }
  }
}

john.calcTips();

console.log(john);

However

let john = {
  fullName: 'John Smith',
  bills: [124, 48, 268, 180, 42],
  calcTips: function() {
    this.tips = [];
    this.finalValues = [];
    for (let i = 0; i < this.bills.length; i++) {
      let percentage;
      let bill = this.bills[i]

      switch (true) { // If I put 'ture' as a parameter, It work's. Why?
        case bill < 50:
          percentage = 0.2;
          break;
        case bill >= 50 && bill < 200:
          percentage = 0.15;
          break;
        default:
          percentage = 0.1;
      }

      this.tips[i] = bill * percentage;
      this.finalValues[i] = bill + bill * percentage;
    }
  }
}

john.calcTips();

console.log(john);

I've searched in google about this problem. But I can't find specific way to solve this issue. I'll appreciate your help.

Upvotes: 0

Views: 58

Answers (2)

Charlie
Charlie

Reputation: 23818

Switch statements compare values strictly. Which means that you can compare for the exact value of the switch variable.

switch (x) {

   case 1: console.log(1); break;

   case 2: console.log(2); break;

}

You can do a trick however if you want to make the switch statement work on numerical ranges like this:

var x = this.dealer;
switch (true) {
    case (x < 5):
        alert("less than five");
        break;
    case (x < 9):
        alert("between 5 and 8");
        break;
    case (x < 12):
        alert("between 9 and 11");
        break;
    default:
        alert("none");
        break;
}

The implementation works on the strict comparison of booleans. The switch statement is for true and will match wherever the case is true.

Related question: Switch on ranges of integers in JavaScript

Upvotes: 1

user12209504
user12209504

Reputation:

The switch statement tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. So in this case you switching on a constant value.

More detail : javascript: using a condition in switch case

Upvotes: 1

Related Questions