10/13


#1

help me

var my_number = 7; //this has global scope

var timesTwo = function(number) {
    my_number = number * 2;
    console.log("Inside the function my_number is: ");
    console.log(my_number);
}; 

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);

#2

Put "var" before your "my_number" (The one inside your function) .


#3

Thank you for your answer, but I didn't quiet understand why outside number is seven and inside number is 14. Can you explain it to me please ?


#4

The outside number is 7 because you put it outside of a function, and as it says from the lesson,
var my_number = 7 //this has global scope

The inside number is 14 because it's in a function, and it comes out as 14 because you multiplied it by 2.

var timesTwo=function(number){
my_number = number * 2;
etc etc
etc etc
};

These are reasons of why it is outside the function and inside the function.

Hope it helped! :smiley:


#5

What this exercise should show you is the idea of shadowing. Here you have to scopes first the "main"-scope outside of the function and then the "local"-scope of the function. In each of them you create a variable my_number. Now you have 2 variables that share the same name but have no connection what so ever. Shadowing now means that by creating a new local variable with the same name the varaible outside of the function is no longer available. Run the code without the var and you see that it accesses the global variable from outside and run it with the var to see that you now have 2 variables.