freeCodeCamp Challenge Guide: Global Scope and Functions

freeCodeCamp Challenge Guide: Global Scope and Functions
0

#1

In JavaScript, scope refers to the visibility of variables. Variables which are defined outside of a function block have Global scope. This means, they can be seen everywhere in your JavaScript code.

Variables which are used without the var keyword are automatically created in the global scope. This can create unintended consequences elsewhere in your code or when running a function again. You should always declare your variables with var.


How can a user declare a variable within a function if established methods don't work?
#2

Only you have to do is declare 2 variables with var and without var as follows. First, declare a variable for myGlobal and give them a value of 10 and another is oopsGlobal without var keyword and give them a value of 5. Then, add myGlobal variable into a function fun1. That’s it. :grinning:

Answer is:

// Declare your variable here
var myGlobal = 10;
oopsGlobal = 5;

function fun1(myGlobal) {
  // Assign 5 to oopsGlobal Here
  var output = "";
  if (typeof myGLobal != "undefined") {
output += "myGlobal: " + myGlobal;
  }
  if (typeof myGlobal != "undefiend") {
output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

// Only change code above this line
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

#3

I completed this piece by doing the following:

I just made X=5 within the function of fun1

// Declare your variable here
var myGlobal=10;

function fun1(x) {
  // Assign 5 to oopsGlobal Here
x=5;  
}
// Only change code above this line
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

Another solution I created was:

// Declare your variable here
var myGlobal=10;
oopsGlobal=5;
function fun1(oopsGlobal) {
  // Assign 5 to oopsGlobal Here
oopsGlobal;  
}
// Only change code above this line
function fun2() {
  var output = "";
  if (typeof myGlobal != "undefined") {
    output += "myGlobal: " + myGlobal;
  }
  if (typeof oopsGlobal != "undefined") {
    output += " oopsGlobal: " + oopsGlobal;
  }
  console.log(output);
}

I’m not sure if this is the kind of asnwer they wanted though…


#4

Hi Mike, I think when you declare a variable without ‘var’, it automatically becomes global.
So, even if you declared ‘oopsGlobal’ inside, its still global without the var.


#5

Your right Omar…! An variable outside of a “function” is a global variable. Even though function fun2, I don’t know the purpose of function fun2()?
// Declare your variable here
var myGlobal=10;
oopsGlobal=5;

function fun1(oopsGlobal) {
// Assign 5 to oopsGlobal Here
oopsGlobal;
}
// Only change code above this line
function fun2() {
var output = “”;
if (typeof myGlobal != “undefined”) {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != “undefined”) {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}


#6

Withdrawn. Didn’t see var wasn’t listed on the answer.

Summary

It’s as simple as adding the two declarations. Do not bother with copying anything from fun(2). Just simply declare the variables in the two different ways.


#7

My solution i tried out

var myGlobal= 10;// Declare your variable here

function fun1() {
// Assign 5 to oopsGlobal Here
oopsGlobal=5;
}

// Only change code above this line
function fun2() {
var output = “”;
if (typeof myGlobal != “undefined”) {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != “undefined”) {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}


"Need Help" Global Scope and Functions 2018
#8

This is how it worked for me!

// Declare your variable here
myGlobal = 10;
oopsGlobal = 5;

function fun1() {
// Assign 5 to oopsGlobal Here
var myGlobal = 10 ;
}

// Only change code above this line
function fun2() {
var output = “”;
if (typeof myGlobal != “undefined”) {
output += "myGlobal: " + myGlobal;
}
if (typeof oopsGlobal != “undefined”) {
output += " oopsGlobal: " + oopsGlobal;
}
console.log(output);
}