Global Scope and Functions Error

Global Scope and Functions Error
0

#1

Tell us what’s happening:
As per the instruction var Myglobal should declare before function but while doing so the code is not getting executed and showing error. But while its defined inside the function its getting executed and its running and showing the output.

What is correct way declare and why this error is coming?

Your code so far

// Declare your variable here
 
function fun1() {
  // Assign 5 to oopsGlobal Here
  var myGlobal=10;
  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);
}

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8.

Link to the challenge:


#2

Your line

var myGlobal=10;

should be immediately under the comment that says

// Declare your variable here

When you declare it inside the function it is actually a local variable rather than a global one. Your code above really should fail, but there is a problem with the tests in this challenge.


#3

Thank You ArielLeslie.