JSFiddle - React, Tailwind, and code Playground

by Ansmtz

JavaScript

const calculateTip = (sum, rate) => {
  let tip = 0;
  switch (rate.toLowerCase()) {
    case "terrible":
      tip = 0;
      break;
    case "poor":
      tip = 5 / 100;
      break;
    case "good":
      tip = 10 / 100;
      break;
    case "great":
      tip = 15 / 100;
      break;
    case "excellent":
      tip = 20 / 100;
      break;
    default:
      tip = -1;
  }
  if (!isNaN(sum) && tip >=0) {
    // nice persons use ceil
    return Math.ceil(sum * tip);
  } else {
    return "Rating not recongnized";
  }
};

console.log(calculateTip(107.65, "great")); // it works
console.log(calculateTip("Test", "good")); // not a good result
console.log(calculateTip(345, "yeopa")); // what?

console.log(calculateTip(78, "good")); // 8
console.log(calculateTip(50, "poor")); // 3
console.log(calculateTip(125, "excellent")); // 25