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

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

#1

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?


I forgot where is the part of the camp where you call a variable inside the function
#2

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


#3

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


#4

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


#5
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.


#6

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


#7

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


#8

I called it by the way :slight_smile:


#9

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:


#10

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.