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

Hi,
Could I get some suggestions on how this function could be refactored to be more streamlined/ efficient?

Thanks,
Chris.

//Write your function here:

const tipCalculator = (quality, total) => {
  switch(quality){
    case "excellent":
          return total *0.3;
    break;
   case "good":
          return total *0.2;
    break;
   case "ok":
          return total *0.15;
    break;
   case "bad":
          return total *0.05;
    break;
   default: return total *0.18;

  }
}





There are other forms of logic, and then there is object lookup…

const rating = {
    excellent: 0.30,
    good: 0.20,
    ok: 0.15,
    bad: 0.05
}
if (rating.hasOwnProperty(quality)) return rating[quality] * total
return total * 0.18
2 Likes

Here’s the code I did below. I keep getting an error message “If ‘ok’ is entered, the function should return 15% of the total passed in” Anyone know what i’m doing wrong?

const tipCalculator = (quality, total) => {
if (quality = ‘bad’) {
return total * .05;
} else if (quality = ‘good’) {
return total * .20;
} else if (quality = ‘excellent’) {
return total * .30;
} else if (quality = ‘ok’) {
return total * .15;
} else {
return total * .18;
}
}

Your code is using an assignment operator to make a comparison. What operator should you be using?

Gotcha, I switched the assignment operator = to comparison operator == and now it says it’s correct. Thanks!

1 Like

Hi !
From reading the questions about this exercise, I wonder if I should have bothered to define x in the first place, but I get my results right, except for this undefined that appears whichever value I pass in. Could anybody indicate what it refers to ?
Thanks in advance.

your tipCalculator doesn’t return anything. You should return the tip amount

the undefined is caused by not returning anything yet attempting to log the returned result when calling the function

1 Like

Great ! Thanks for the tip ! :grinning:

I got a tick for this code but just curious why I got ‘NaN’ as terminal output?

const tipCalculator = (quality, total) => {
  let tip;
  switch(quality){
    case 'bad':
    return tip = total * 0.05;
    break;
    case 'ok':
    return tip = total * 0.15;
    break;
    case 'good':
    return tip = total * 0.2;
    break;
    case 'excellent':
    return tip = total * 0.3;
    break;
    default:
    return tip = total * 0.18;
    break;
  }
}

console.log(tipCalculator('good, 100'));

NaN is the result of multiplying a number by a string.

Hello
Could someone help me as i don’t know where i am wrong and I cannot continue ?

it is printed totalCost * .05 ( not a number ) why ?

thanks

Your return values are string objects. The computation never takes place. Have you considered using expression interpolation?

return `Tip is ${totalCost * 0.05}`

You can remove the break from all the cases since you are using return. Anything after return is unreachable.

Consider the following use case for break

const tipCalculator = function (totalCost, quality="") {
  let pct;
  switch (quality) {
    case 'bad': pct = 0.05; break;
    case 'ok': pct = 0.15; break;
    case 'good': pct = 0.20; break;
    case 'excellent': pct = 0.30; break;
    default: pct = 0.18
  }
  return `Tip amount is ${totalCost * pct}`
}
for (let x of ['bad', 'ok', 'good', 'excellent']) {
  console.log(tipCalculator(100, x));
}
console.log(tipCalculator(100));
Tip amount is 5
Tip amount is 15
Tip amount is 20
Tip amount is 30
Tip amount is 18

Thank you very much mtf. Now I understood it was an error due to string objects. I also didn’t know that I didn’t need to use ‘break’ if I used already ‘return’, very good point I know now. In the beginning, I used interpolation ( not like your code though ), I used it in every ‘return tip is …’. but it didn’t work. So I deleted and tried new one which was above code. I think my expression interpolation signs are not working properly in most of the projects and I needed to copy and paste of exact interpolation sign from others ( eg: ${}), then it worked. I don’t know the reason why Expression Interpolation sometimes was not working. I like your code and it is much clear and different. Thanks again, I learnt an alternative way of code.

1 Like

You’re welcome. Use it as a reference, though, not a solution. We should scratch code from our knowledge base, and over time we will arrive at our own variations and refactoring approaches. Let it grow naturally within your mindset. During review is a good time to challenge yourself to revise the original code (that passed) just so you get practice with other constructs and techniques. What you grow yourself sticks much better than other examples, such as above. Take away what you wish, and study or explore it, just don’t use the solution now that you’ve seen it.

1 Like

yes , i will do so . I am always curious all other different methods of codes and of course they are as a reference. Thanks for your advice mtf.

1 Like