Quiz: Javascript Review


I'm fairly sure that one of the questions and the respective answer in this quiz is incorrect...but if not please tell me why.

The question is 'How many local variables are there in the following block of code?'

var input = prompt("Enter input value");
var controlVal = input / 2 + 3;
var multiplier = function(number, phase) {
  var val = number *  controlVal + phase;

I thought the answer should be 1 as the rest are all global variables. However the accepted answer is 3. Surely this is wrong?

I'm even more convinced when a further question in this section asks: What type of variable is val in the block of text below?

var input = prompt("Enter input value");
var controlVal = input / 2 + 3;
var multiplier = function(number, phase) {
  var val = number *  controlVal + phase;

And the accepted answer is local.

Please could someone check this out and either explain why I'm wrong or correct the quiz?



Nope, for the first question, 3 is the right answer. Any variable that start with the keyword var is a local variable, so there are 3: input, controlVal and val. If you wanted for example input global:

input = prompt("Enter input value");

This should also your second question. val indeed is a local variable


Thanks for the reply.

So what is a global variable if local variables starts with the keyword var?


I seemed to have got this wrong. Variable without or without var keyword are global if not in a function:

var x = 3; /* global */
y = 3; /* global */

and inside a function:

var my_function = function(){
  var x = 3; /* local */
  y = 3; /* global */


So my original theory was correct then that the answer should be 1 local variable and not 3?

If so can the quiz be updated for future participants?



I am not paying attention, the answer 3 is correct. Wow i am sleepy. Number and phase are also local (they only exist inside the function)


Yes but number and phase are parameters not variables.

val is the only local variable in the function.
input, controlVal and multiplier are all global variables that exist outside the function (although multiplier is the name of the function itself so I'm not sure if this counts...?)

Again, I believe there is only 1 local variable and therefore the answer should be 1.


stackoverflow doesn't agree with you:
There is no difference. A parameter is just a local variable which is initialized with the passed argument at invokation time.

So 3 is the correct answer?
@cadecodes, w3schools? :open_mouth: :frowning:


I think they are more reliable for web than stackoverflow...


From W3Schools:

Function Arguments

Function arguments (parameters) work as local variables inside functions.

In which case, fair enough. Thanks for the tip off. 3 is the correct answer in that case. I don't remember covering that parameters also count as local variables in the Codecademy course, but I'll be sure to remember now.

Thanks again


Is this the question you are referring to?

Please note the difference between your quote and the actual question.

Perhaps codecademy has changed the question?
However, like you, I also feel that this particular example isn't accepting the correct answer; which I think is 2.

Can anyone else clarify that this quiz hasn't been changed by codecademy since 11 March?


The question was how many local variables, not global

There are 3 local variables.


@stetim94, please have a look at this screen shot from the quiz I believe the OP's question is related to.

If there were 3 local variables then "input" would be a correct answer.

There is only 1 local variable: "val".


input is a global variable, as explained in my earlier answers. val is the local variable, and the function parameters are technically also considered local variables


For the purposes of the quiz, the parameters of the function are not considered local variables.


i have had to pick only 1, i would obviously choice val. I agree that doesn't look really good, i will ask around to see who has up to date knowlegde of the quizzes, been a while since i completed them


I think there is a possible bug in the quiz, as shown in my post above to the OP ( regarding the number of global variables being acknowledged as 3 ).

It would be helpful if someone could look into this.