Basic JavaScript - Global Scope and Functions

Tell us what’s happening: seems right yet getting error
Describe your issue in detail here.

  **Your code so far**
// Declare the myGlobal variable below this line
var myGlobal = 10;

function fun1() {
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:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

oopsGlobal is not accessible globally since it is in a function.

Please ask a question if something is confusing you.

the tests tell you what the issue, try reading them

thank you. makes sense

thank you. I will once I figure out how to ask it with a bit of knowledge. working through it

Are you sure about that?

function fun1() {
  oopsGlobal = 5;
}

fun1()
console.log(oopsGlobal) // 5

One of the learning points of the challenge is that if you do not declare a variable inside a function but assign it a value, then it IS declared in the global scope.

If you rewrite the above code and declare oopsGlobal inside the function with var, let, or const, the code will error out with ReferenceError: oopsGlobal is not defined because it is not in global scope.

function fun1() {
  var oopsGlobal = 5;
}

fun1()
console.log(oopsGlobal) // ReferenceError: oopsGlobal is not defined

honest, been trying. I don’t understand.

thank you. I will once I figure out how to ask it with a bit of knowledge. working through it

Being able to talk about code is an important part of being a coder, even explaining what you don’t understand. Quote the description if you need to. Quote the code that you don’t understand if you need to. But without that, we are left trying to explain everything.

There is nothing wrong with getting hung up on something - we’ve all been there. Asking questions is a good thing. But you have to ask the question to get an answer. If you don’t know how to ask a good question, then ask a bad one so you can get better at asking questions.

1 Like

ok. code below. it is still giving me the error of - oopsGlobal should be a global variable and have a value of 5 - yet when I change it to 5 it says it is an error. it is not logical to me.

// Declare the myGlobal variable below this line
let myGlobal = 10;

function fun1() {
  oopsGlobal = 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);
}
fun1();
fun2();```

please show the code of when you assign it a value of 5

thanks @ilenia - triggered something. I was changing the top, not the function. It passed.