Help with function 5/7


#1

Codecademy doesn’t show any error message.
but the output is just 0. I don’t understand how my orderCount will increment.
Link to the exercise
https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/return?action=lesson_resume&link_content_target=interstitial_lesson

Here is my code:

var orderCount=0;
function takeOrder(topping, crustType){
  return ('Order: '+ crustType+ ' crust pizza tooped with '+topping);
  orderCount = orderCount+1;
}

function getSubTotal(itemCount){
  return itemCount*7.5;
}
takeOrder('cheese','thin');
takeOrder('bacon','thick');
takeOrder('mashroom','slime');
console.log(getSubTotal(orderCount));

#2

return terminates a function so whenever return statement is reached it ends at that moment.

so orderCount should be before return statement


#4

Hi @cssace36527,

@rcodeman’s explanation on the return is very helpful to clarify your situation on why the orderCount won’t increase.

That aside, I referred back to the exercise (got me curious).

Function 2/7: https://www.codecademy.com/courses/learn-javascript/lessons/functions/exercises/functions

The exercise’s instruction stated:

1 .
Imagine you work at a pizza restaurant and you want to write a JavaScript program to take orders so you don’t have to write them out by hand. You can write a function to perform this task!

Start by writing a function named takeOrder. Inside of its block, use console.log to print ‘Order: pizza’.

It’s better to keep following the instructions at early stage, so you can get the green tick and let you pass to the next exercise. You won’t be facing this issue if you use console.log as it does not exit the function.

I understand the exercise let you passed, sometimes the checking system can’t check for everything, it’s great you’re observant of your output. :smiley:


Using return is no harm if you follow @rcodeman explanation, just mentioning it for the sake of this exercise.

Happy coding. Cheers! :slight_smile:


#5

ok. i’ve solved the problem.
The solution is

var orderCount=0;
function takeOrder(topping, crustType){
  console.log('Order: '+ crustType+ ' crust pizza tooped with '+topping);
  return orderCount = orderCount+1;
}

function getSubTotal(itemCount){
  return itemCount*7.5;
}
takeOrder('cheese','thin');
takeOrder('bacon','thick');
takeOrder('mashroom','slime');
console.log(orderCount);
console.log(getSubTotal(orderCount));

thanks for your help


#6

Thanks for your helpful tips :slight_smile:


#7

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