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

Continuing the discussion from freeCodeCamp Challenge Guide: Global Scope and Functions:

// Declare your variable here
var myGlobal = 10;
-------------------------------------------------------
function fun1(oopsGlobal) { 
  // Assign 5 to oopsGlobal Here
  oopsGlobal=5;}
fun1(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 get the concept as described. I would prefer using let or const but the system does not support that (which I think is a little silly).
I’m using free code camp alongside codeacademy, so I’ve done 30 hours of review on this particular topic including while loops .

Any suggestions?

note that I have isolated the section I am focusing on (the middle section)

why are you using oopsGlobal as function argument? the challenge ask only to assign a value to it inside fun1.
the idea behind this challenge is to focus on difference between declaring variable with var keyword or not.
check this link

1 Like

my impression is that that’s how you could avoid calling it a variable…let me check out the link.

var myGlobal = 10;

function fun1() { 
  // Assign 5 to oopsGlobal Here
  oopsGlobal= 5; console.log(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);
}

Above is the correct version. Is this just showing that oopsGlobal is only local? And that myGlobal is a globally declared variable?

I just want to make sure I’m learning the correct concepts for later.

try a call to fun1 and you’ll see that oopsGlobal is GLOBAL::wink:

Whats the differenc ebetween that and using var for global? Is it just like a loophole that programmers haven’t figured out?

I called it by the way :slight_smile:

I know the answer - it makes sense, I think:

// Declare your variable here
var myGlobal = 10;

function fun1() {
// Assign 5 to oopsGlobal Here

oopsGlobal= 5; console.log(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);
}

:smirk:

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

function fun1(oopsGlobal) {
// Assign 5 to oopsGlobal Here
oopsGlobal = 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);
}

this is my idea.