Why is my function "undefined"?


#1

When I called the total function in the pizza exercise, I don’t know why but I am keep getting “undefined” as a result. What is the problem of my code?

let orderCount = 0
const takeOrder = (topping,crustType) => { orderCount +=1
  return `Order: ${crustType} pizza with ${topping}.`;
};

const getSubTotal =(itemCount) =>
{
  return itemCount * 7.5;
};


const getTax = (orderCount) => {
  return
  getSubTotal(orderCount) *0.06;
  };

const getTotal = () => {
  return 
  getSubTotal(orderCount)+ getTax(orderCount);
};

console.log(takeOrder('mashroom','thick crust'));
console.log(getSubTotal(orderCount));
console.log(getTotal());

#2

this:

  return
  getSubTotal(orderCount) *0.06;

has to be on a single line, otherwise the default value is returned

if you want to put it on multiply lines, use parentheses:

return (
  getSubTotal(orderCount) *0.06;
)

#3

Omg ! It worked.!
It did not work because I was returning noting.
From now on I will pay more attention to the lines.
Thank you so much!


#4

that can’t be, value has to be returned, so very like undefined (the default returned value)

you’re welcome :slight_smile:


#5

Hi @webmaster82737,

This situation happened because of how JavaScript operates, one mechanism which referred as Automatic Semicolon Insertion:

Specifically for return case
https://developer.mozilla.org/my/docs/Web/JavaScript/Reference/Statements/return#Automatic_Semicolon_Insertion

If the value is omitted, undefined is returned instead.

For more reading about ASI (It does not only affect return but others as well):

Just sharing the relevant documentations here. Happy learning! :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.