Function Calculation Errors

Question:

What is wrong with the following code. Each attempt to run this code brings back syntax error. What is missing and needs correction here?
Repl link:

function calculateFoodTotal(food, tip) {
  tipPercentage = tip / 100
  tipAmount = food * tipPercentage
  total = sum(food, tipAmount) 
  return total
}

console.log(calculateFoodTotal(100, 20))

Can you please provide the full error, (As text) and the repl link?

3 Likes

First of all, while it is technically not required, it is good practice to use semicolons. Second, where is sum defined? I suspect you’re attempting to use the Python function by mistake. Either way, you have two numbers and you’re trying to add them; you should be using the addition operator (+).

3 Likes

Even if sum is not defined, that wouldn’t be a SyntaxError, right?

2 Likes

No, but just looking at the code provided, there is no syntax error. If you run that in your browser console it’ll tell give you an UncaughtReference error because sum is undefined.

2 Likes

This is the error message

ReferenceError: sum is not defined https://5e18ab47-26e3-4c22-9c3e-73286f2c6588.id.repl.co/yourPlayground.js:68

at calculateFoodTotal (https://5e18ab47-26e3-4c22-9c3e-73286f2c6588.id.repl.co/yourPlayground.js:68:3)
at https://5e18ab47-26e3-4c22-9c3e-73286f2c6588.id.repl.co/yourPlayground.js:72:13

AFAIK sum does not exist in JS no?

Just do:

total = food + tipAmount; 
5 Likes

try

function calculateFoodTotal(food, tip) {
 const tipPercentage = tip / 100
 const tipAmount = food * tipPercentage
 const total = food + tipAmount
  return total
}

console.log(calculateFoodTotal(100, 20))
2 Likes