FAQ: Functions - return

This community-built FAQ covers the “return” exercise from the lesson “Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn JavaScript

FAQs on the exercise return

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

On this exercise, why is the argument used in the getSubTotal function called itemCount? I understand that the orderCount variable ultimately gets passed in, but wouldn’t it have been simplest to just make orderCount the parameter that was used in the getSubTotal function in the first place? It seems like that works fine and is less complicated.

Where simple values are concerned parameters are independent of the argument given.

function foo(orderCount) {

}

foo(orderCount)

The orderCount variable in the argument is not the same variable as the one in the parameter, though it looks the same. They are in two different scopes. A parameter of the same name as the argument is said to shadow the global object meaning it is inaccessible from inside the function. What we call the parameter is completely arbitrary. Only the value is passed in, not the reference.

You’ll find that this is not the case with data structures such as arrays and objects. They are never shadowed as only the reference is passed into the function. This will come up in due course. For now, consider that orderCount is a number in global scope, and any parameter that takes it will be in function scope using the local name to refer to the value.

This exercise is attempting to give us a feel for how arguments and parameters work together, and how scope difference places variables of the same name in two different universes.

Wow thank you so much! Like you said, “This will come up in due course.” And I will save this email for when that time comes. I just really appreciate this as it’s given me something to chew on as I’m getting more comfortable with this language. Thanks again!

1 Like
var orderCount = 0;
function takeOrder(crustType, topping) {
  console.log('Order: ' + crustType + ' crust topped ' + topping);
  orderCount=orderCount+1;
}
function getSubTotal(itemCount) {
	return itemCount * 7.5;}
takeOrder('thin.', 'Sausage');
takeOrder('Chicago Style.', 'Pepperoni');
takeOrder('Brooklyn Style.', 'Ham');
function takeOrder(topping, crustType){
 console.log('Order: A '+crustType+' Pizza topped '+topping)}
takeOrder('normal.','Pepperoni')
var numberPizzas=undefined;

Yet step 2 counts me wrong!?!

Why the second takeOrder function?

Is that variable even called for? To set a variable to undefined, don’t set it, just declare it.

var numberPizzas;

oh i saw this in the first exercise so assumed that that would be the way to declare

It’s not wrong, just not necessary. In most instances we can test for truthiness, unless there is a possibility of some other value being there such as, 0 that we need to include as a valid state.

console.log(function(){})

What was logged? We will find undefined all over the place, without ever having to actually hard code it.

Might we at some time need to verify it with certainty as being the literal, undefined? Perhaps, but rarely is that the case in my experience. Single out these edge cases when you see them and inquire whether there is some other way to make the same determination. That way you have two tools in you kit, instead of just one.

Please don’t let me contradict your lessons. Just know that in a brief lesson, it is impossible to explore down every avenue. That is something we will get to in time, and with lots of study, exploration and practice.

var orderCount = 0

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

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

takeOrder(‘bacon’, ‘thin crust’);
takeOrder(‘bacon’, ‘thin crust’)
takeOrder(‘cheese’, ‘thick crust’)

console.log(getSubTotal(orderCount))

this is my Code, it says everything, i get checkmarks but I dont get a result my result is:
Order:thin crustpizza topped withbacon
Order:thin crustpizza topped withbacon
Order:thick crustpizza topped withcheese
0

But I should get the SubTotal

1 Like

When you use === you are comparing the values on each side. To assign a value to orderCount you only use =. = is the assignment operator.=== is a comparison operator. If you tried console.log(orderCount === orderCount + 1) you would see what is happening. You are evaluating whether orderCount is the same as orderCount +1. It isn’t, so false would be logged to the console. Happy coding!