Undefined getSubTotal()


#1

I’m on Part 5 of Lesson 4 the console is returning an undefined message after I run this code below. Seems a bit odd since I can’t see any syntax errors and the I’m checking that I’m logging four orders in the orderCount - what am I doing wrong?

let orderCount = 0;
const takeOrder = (topping, crustType) => {
  orderCount = orderCount + 1;
};

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

takeOrder('mushrooms','thin crust');
takeOrder('pepperoni','stuffed crust');
takeOrder('pineapple','stuffed crust');
takeOrder('anchovy','deep pan');
console.log(orderCount);
console.log(getSubTotal(orderCount));

#2

Hi @apache1,

This happens because of what’s referred as JavaScript’s automatic semicolon insertion (ASI)

You can refer more about it here (Search for automatic semicolon insertion):
https://developer.mozilla.org/my/docs/Web/JavaScript/Reference/Statements/return

To avoid this problem, you can either make code in the same line after the return word OR using parentheses as what the documentation suggested. In this case, I think it is much easier to write the code in single line (since it’s short). The ASI is what causing the undefined.

const getSubTotal = (itemCount) => {
  return                         //<=== This part
  itemCount * 7.5;
};

On side note:

const takeOrder = (topping, crustType) => {
  orderCount = orderCount + 1;
};

Did you miss out the console.log to print those sentences of ordering food inside the takeOrder function?

Cheers :slight_smile:


#3

I answered my own question just as you responded! Thanks for the quick response though! Which parenthesis [] {} () ?

And no I just removed the console.log for the orders temporarily


#4

@apache1,

Have a look on the link I provided, you will understand what I said about parentheses. Search under the Automatic Semicolon Insertion section and read the example given, you will see what needs to be remedied.
:slight_smile:


#5

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