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
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
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 ...
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, ...
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,...
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 ...