Need help with this code please

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

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

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);
}

console.log(oopsGlobal);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43

Challenge: Global Scope and Functions

Link to the challenge:

Look at the tests.

myGlobal should be declared using the let or const keywords

It is quite convoluted. The key concept is to understand variables scoping. It is a large topic.

Good practice for defining variables is such that they only exist inside the block of code they were defined. For example,

if(true){
const a='hello'
var b='world'
}
console.log(a,b)

const and let are always scoped to a block { //code... }. var is only scoped to function blocks. And with no keyword, the variables are global.

With all this data you can probably interprete what they try to show.

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