Working on the below, when logging the array 'tips' it seems that the numbers pushed into it have only one decimal point.
Before adding in parseFloat it would return 2 decimal points, however it was returned as a string. Once parseFloat was added it now only seems to return a single decimal point.
Thank you.
var tips = [];
var calculateTip = function(bill) {
switch (true) {
case bill < 50:
tips.push(parseFloat((bill * 0.2).toFixed(2)));
break;
case bill >= 50 && bill < 201:
tips.push(parseFloat((bill * 0.15).toFixed(2)));
break;
default:
tips.push(parseFloat((bill * 0.10).toFixed(2)));
}
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);
Numbers in Javascript are output to console with as many decimal places as needed.
console.log(1.0000001)
console.log(1.1)
All your numbers only display with one decimal point as they only have one decimal place. You should return a string instead if you want to show them with a specific precision.
var tips = [];
var calculateTip = function(bill) {
switch (true) {
case bill < 50:
tips.push((bill * 0.2).toFixed(2));
break;
case bill >= 50 && bill < 201:
tips.push((bill * 0.15).toFixed(2));
break;
default:
tips.push((bill * 0.10).toFixed(2));
}
}
calculateTip(124);
calculateTip(48);
calculateTip(268);
console.log(tips);