Functions: return pizza.js incorrect answer


#1


Functions 5. return - 5


It works fine but it says incorrect.. Is it something I did wrong?


var orderCount = 0

function takeOrder(topping, crustType) {
  orderCount = orderCount + 1;
  console.log('Order: ' + crustType + ' crust topped with ' + topping);
}

takeOrder('bacon', 'thin');
takeOrder('pepperoni', 'regular');
takeOrder('pesto', 'thin');

function getSubTotal(itemCount) {
  return itemCount * 7.5;
  
}

console.log (getSubTotal(orderCount));


#2

Hi @coresurfer31626,

Your code seems fine.

Just that I noticed:
1. Try add a semicolon ; after
var orderCount = 0
to
var orderCount = 0;

2.Remove the white space between (I suspect this is the culprit)
console.log (getSubTotal(orderCount));
to
console.log(getSubTotal(orderCount));

Both the above observations do not affect the code at all, but maybe the checking system is looking for that.


#3

Have you tried replacing "itemCount" with "orderCount"?

I just went and played with it both ways and both produce the same result for the subtotal in the console as long as you are consistent and use the same term in declaring the function and in the function block.

Honestly I am puzzled as to why "itemCount" works at all since it hasn't been defined. That is how the instructions were written but I assumed it was a typo when I was doing the exercise and used "orderCount" and it let me pass. I also tried replacing "itemCount" with gibberish and it still produced the correct result for the subtotal (again, as long as the terms are the same in function and the function block).


#4

Hi @rowan8,

itemCount is just a parameter. Therefore, you can just replace anything in place of itemCount, such as:

function getSubTotal(abc) {
  return abc * 7.5;
}

or

function getSubTotal(onetwothree) {
  return onetwothree * 7.5;
}

Just like you mentioned, replacing "itemCount" with gibberish, the code would still work. You can write anything in place of parameter as long as it follows the logic in the code. In this case, if you write countingCounter as parameter, you have to return countingCounter * 7.5 .

Whether the lesson let you pass or not, that is another matter which has to depend on the checking system.

You mentioned why "itemCount" hasn't been defined but it works, because it just serves its purpose as a parameter for a function. Parameter is not a global variable, it is locally defined and scoped to the function, thus it is not defined outside the function.

On the other hand, orderCount is a global variable outside the function, defined by
var orderCount = 0;

The purpose of avoiding using orderCount or any same Name as a parameter when it exists as a variable itself is to reduce confusion or accidental error when the code starts getting messy.


For more reading about parameter:
You can refer:

and


itemCount value?
#5

Hi @codexthon,
Thank you for the very clear explanation, it makes sense now :grin: The math works because when we actually call the function to get the subtotal we are using the variable orderCount that we defined at the beginning.

@coresurfer31626: sorry for the thread hijack. I hope you find a solution to your problem.

Rowan


#6

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