FAQ: Code Challenges: JavaScript Fundamentals - tipCalculator()

Don’t round your answer, return as it is…it will work fine

Hi, What’s wrong with this code?
its showing

If ‘bad’ is entered, the function should return 5% of the total passed in


function tipCalculator(quality,total){

  switch(quality){
    case 'bad':
      return `you got tip ${total*0.05}`;
    case 'ok':
      return `you got tip ${total*0.15}`;
    case 'good':
      return `you got tip ${total*0.20}`;
    case 'excellent':
      return `you got tip ${total*0.30}`;
    default:
      return `you got tip ${total*0.18}`;
  }
}

console.log(tipCalculator('good', 100)) //should return 20


Update

Code got approved when I removed `` from the code and just simply return total*0.x
But why my previous code was not approved? both have same result

return a number or a string are different data types. If you would need the calculated number later, having it in a string creates extra challenges.

1 Like

Console detects error but when I test the syntax it works fine. Anyone help??

const tipCalculator = (quality, total) => {

  switch (quality) {

    case "bad":

    return total * 1.05;

    case "ok":

    return total * 1.15;

    case "good":

    return total * 1.2;

    case "excellent":

    return total * 1.3;

    default:

    return total * 1.18;

}};

the function should return just the tip, not the whole price + tip.

1 Like