Functional Scope


#1

Hi,
I have been stuck at this exercise for a few days now, no matter what I do I cannot get through the third instruction. I have even copy pasted the code from the instruction into the code to see if I that helped. can anyone tell my how to get past this?

https://www.codecademy.com/en/courses/learn-javascript/lessons/scope/exercises/functional-scope?action=resume

here is my code: 
var laundryRoom = (' In-unit ');
var mailRoom = (' Room 1A ');

function myApartment(mailBoxNumber = 'Box 3') {
console.log('Mail box: ' + mailBoxNumber + ', Laundry:' + laundryRoom);
}

thanks :slight_smile:


#2

function myApartment(mailBoxNumber = 'Box 3') { // you cant give value like this

Function parameters are the names listed in the function definition.

`function functionName(ParameterName){
     //code to be executed
}

now after closing the function call it by function name giving argument.Function argument is the real value received by the function when it is invoked.

functionName(argument) //calling function

so your code should be like

function myApartment(mailBoxNumber) {
console.log('Mail box: ' + mailBoxNumber + ', Laundry:' + laundryRoom);
}
myApartment('Box 3') //calling function

#3

that still doesnt get me through the exercise.

it want's me to do so in the second instruction. according to the instructions, I only have problems with the control.log part of my code for some reason


#4

myApartment() takes no parameters. The variables are locally declared and defined.

  var mailBoxNumber = 'Box 3';
  var laundryRoom = 'In-unit';

#5

2.
Inside of the function, write a variable named mailBoxNumber and set it equal to Box 3.

^ that is the second task I have been given.
according to the instructions I am supposed to have a parameter inside the function. my problem is what to do to get through the third task

3.
Inside the function, use console.log() to print out both variables, like this:

console.log('Mail box: ' + mailBoxNumber
+ ', Laundry:' + laundryRoom);


#6

This may be interpreted to mean in the function body and should not imply a parameter. The purpose of this exercise it to demonstrate the difference between global and local scope.

Scope refers to access. Variables outside a function (those in global scope) are accessible inside a function. Those inside a function are only accessible within the function, and not to the outside.

var laundryRoom = 'Basement';
var mailRoom = 'Room 1A';

function myApartment() {
    var mailBoxNumber = 'Box 3';
    var laundryRoom = 'In-unit';
    console.log('Mail box: ' + mailBoxNumber
+ ', Laundry:' + laundryRoom);              // Mail box: Box 3, Laundry:In-unit
}
console.log('Laundry: ' + laundryRoom +  ', Mail: ' + mailRoom); 
                                            // Laundry: Basement, Mail: Room 1A

myApartment();

The function body is delimited by the curly braces. The two variables declared in the body of the function are not accessible outside of that scope.

console.log(mailBoxNumber);   // ReferenceError: mailBoxNumber is not defined

Note the difference in the outputs. See how inside the function, the variable laundryRoom refers to the local value, 'In-unit', whereas outside it refers to the value, 'Basement'?


#7

I had never figured that out o.o

thank you so much for your help!! it worked, and makes a little more sense now ^^


#8

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