Set varbiables global - codecamdemy not satisfied


<Below this line, add a link to the EXACT exercise that you are stuck at.>

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>

I am shure its just a minor issue, but I cant seem to find the error.
The task is, to make all the necessary variables inside the function accessable to the console.log statement at the end (outside the function). I thaught that would be easy, since the only thing i have to do, is to remove the “var” in front of them. This should automatically set them to global and make them accessable to the console.log statement outside the function. Except it somehow does not :confused:


function myApartment() {
var myCoffeeMaker = ‘Aeropress’;
buildingAddress = ‘150 E 14th St, New York, NY’;
var myCloset = ‘Extra coats in the back’;
buildingLaundryCode = 4927;
var myRefridgerator = ‘Filled with veggies and dark chocolate.’;
var myDog = ‘Nikko’;
buildingPhone = ‘(481) 516-2342’;

// Do not edit the code after this line
console.log(“Apartment Building Information”);
console.log("Laundry code: " + buildingLaundryCode + "\nPhone: " + buildingPhone + "\nMailing address: " + buildingAddress);

<do not remove the three backticks above>

Since the variables are in the function, this makes them private to just that function.

Global variables should not be in functions.

Hope that helps.

Much to my regret it does not help ion this case.

NOT declaring global variables inside a function may be “best practice”.

But from a pure technical point of view, my solution should still work.

Acconding to MDN and W3schools, if you declare variables inside a function WITHOUT the “VAR” , they should be global.

See here for instance:

(Automatically Global
If you assign a value to a variable that has not been declared, it will automatically become a GLOBAL variable.)


Yes, you are correct. However, under further examination, the variable is
only available when called. For example, if I have…
function myFunc() {
var global = “I wanna be global!”;

The variable global is only accessible if I call it before I define it.
Weird huh?

Tell me if that makes a difference.


1 Like

Thank you for this elaboration.

I cant test whether it satisfies the codecademy exercise or not, but I did a quick test in my JS Editor and there my global function INSIDE the variable was not recognizes by the rest of the code unless I called the function befire I defined it. Just as you said.

Awesome! I’m glad it worked out!

In the future, it is usually easier and convient to place variables outside
of functions (in order to make them global).

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