JavaScript why I get Different result in switch statements

I am learning JavaScript and currently I build simple tip calculator using function and switch statement. Basically, tip 20% of the bill when the bill is less than $50, 15% when the bill is between $50 and $200, and 10% if the bill is > $200. The sample input/argument:

simpleTipCalculator(124)
simpleTipCalculator(48)
simpleTipCalculator(268)

if you calculate manually, the result(expected result):

18.599999999999998
9.600000000000001
26.8

Here is my code so far:

function simpleTipCalculator(bill){
    let tip = 0
    switch(bill){
        case bill > 0 && bill < 50:
            tip = bill * 0.2
            break;
        case bill >= 50 && bill <= 200:
            tip = bill * .15
            break;
        default:
            tip = bill * .1

    }
    console.log(tip)
}

and the result of this function:

12.4
4.800000000000001
26.8

I feel confused about this and then I change my code to this:

function simpleTipCalculator(bill){
        let tip = 0
        switch(true){
            case bill > 0 && bill < 50:
                tip = bill * 0.2
                break;
            case bill >= 50 && bill <= 200:
                tip = bill * .15
                break;
            default:
                tip = bill * .1

        }
        console.log(tip)
    }

The result is what I expected.

18.599999999999998
9.600000000000001
26.8

My question is how did this happen? please explain me about that because I don't know what to look for on Google about this

Answer:1

switch case does not work the way you are using it.

switch(test){
    case a:
        break;
    case b:
        break;
    case c:
        break;
}

compares the value of test to the value of a, then b and so on

so in your first code example it compares 124 with the value/output of (bill > 0 && bill < 50) which is false

so you are comparing integers with booleans.
To make ranges work you have to do it like in your second example.

Stackoverflow post I found: https://stackoverflow.com/a/5619997/7584725

Answer:2

I have an array which looks like:- [[0,1], [0,2], [0,3], [1,1], [1,2]...] I am looking to remove one of the arrays from this array based on the indexOf() but I keep getting a value of -1, which ...

I have an array which looks like:- [[0,1], [0,2], [0,3], [1,1], [1,2]...] I am looking to remove one of the arrays from this array based on the indexOf() but I keep getting a value of -1, which ...

  1. remove array from array
  2. remove array from array python
  3. remove array from excel
  4. remove array from object javascript
  5. remove array from array php
  6. remove array from json object
  7. remove array from list python
  8. remove array from array java
  9. remove array from array swift
  10. remove array from another array python
  11. remove array from json object javascript
  12. remove array from multidimensional array php
  13. remove array from index javascript
  14. remove array from array typescript
  15. remove array from object
  16. remove array from array ruby
  17. remove array from array javascript lodash
  18. remove array from array numpy
  19. remove array from multidimensional array javascript
  20. remove array from object php

I am trying to import several thousand records into a nested array in a collection in Meteor. This is financial data coming in a JSON object. I need to do some calculations on it before insertings it, ...

I am trying to import several thousand records into a nested array in a collection in Meteor. This is financial data coming in a JSON object. I need to do some calculations on it before insertings it, ...

  1. meteor bulk insert
  2. meteor mongodb bulk insert

I am working on one input application where i need to test input values that accepts single, multiple and even a range of numbers . Eg inputs : 70,900,80-20 // should return true as all are valid as,...

I am working on one input application where i need to test input values that accepts single, multiple and even a range of numbers . Eg inputs : 70,900,80-20 // should return true as all are valid as,...

  1. angular test input values

i created a crypto object as follows: var crypto = { encrypt: function(s) { } }; crypto.encrypt("cat"); I would get the following error Uncaught TypeError: crypto.encrypt is ...

i created a crypto object as follows: var crypto = { encrypt: function(s) { } }; crypto.encrypt("cat"); I would get the following error Uncaught TypeError: crypto.encrypt is ...