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());
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;
)
1 Like
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!
1 Like
that can’t be, value has to be returned, so very like undefined
(the default returned value)
webmaster82737:
Thank you so much!
you’re welcome
1 Like
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):
This page describes JavaScript's lexical grammar. The source text of ECMAScript scripts gets scanned from left to right and is converted into a sequence of input elements which are tokens, control characters, line terminators, comments or white...
Just sharing the relevant documentations here. Happy learning!
system
closed
March 8, 2018, 8:25am
#6
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.