Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scope Problem is Confusing #125

Open
michaelgrilo opened this issue Sep 8, 2015 · 3 comments
Open

Scope Problem is Confusing #125

michaelgrilo opened this issue Sep 8, 2015 · 3 comments

Comments

@michaelgrilo
Copy link
Contributor

As someone who has no previous JavaScript knowledge, the Scope problem is quite frustrating. Even though the introduction does a good job defining scope, I was not prepared to follow the code example.

Comments need to be added next to console.log to indicate that when we pass the argument within bar back to the function, var b = 2 will be ignored. I'm sure I haven't phrased that well, but it's a starting point.

Also, what purpose does the line foo(); // 4, 2, 48 serve? I understand that foo is the function's name, and the comments next to it are the values of these variables, but this statement is just sitting there. We need context. What would including foo(); at the end of this program do when we run it?

I also don't believe the IIFE section adds any relevant information to this problem. It is distracting.

Now moving on to the challenge, I don't understand why there is )(); after every closing }. I suppose that ties back to the foo(); in the example code, but I don't understand its usage.

@sethvincent
Copy link
Collaborator

Hey @michaelgrilo, this is really helpful! I agree that the IIFE stuff is distracting.

I'll write a revised version and will post it here later to see what you think.

@sethvincent
Copy link
Collaborator

Here's an attempt at a much simpler version of a scope problem.

Let me know what you think.


SCOPE

Scope is the set of variables, objects, and functions you have access to.

JavaScript has two scopes: global and local. A variable that is declared outside a function definition is a global variable, and its value is accessible and modifiable throughout your program. A variable that is declared inside a function definition is local. It is created and destroyed every time the function is executed, and it cannot be accessed by any code outside the function.

Functions defined inside other functions, known as nested functions, have access to their parent function's scope.

Pay attention to the comments in the code below:

/* global variables are created outside functions */
var hello = 'this is global';

/* local variables are created inside functions */
function changeGlobalVariable () {
  var hi = 'this is local';
}

/* this will return 'this is global' */
console.log(hello);

/* 
* this will return the error `ReferenceError: hi is not defined`, 
* because `hi` is defined inside a function and is not available in the global scope 
*/
console.log(hi);

The challenge:

Create a file named scope.js.

In that file, copy the following code:

/* this is a global variable definition, because it is outside a function */
var aGlobalVariable = 'a global variable';

/* 
* define a function with two local variable definitions, 
* including usage of a function that returns a value
*/
function iAmAString () {
  var me = 'i am a string'
  var local = aLocalVariable();
}

/* define a function that returns a local variable */
function aLocalVariable () {
  var local = 'a local variable';
  return local;
}

/* this runs the iAmAString() function so that everything inside the iAmAString() definition is executed.  */
iAmAString();

Use your knowledge of the variables' scope and add a console.log statement so that the output is:

'i am a string constructed from a local variable & a global variable'

Your answer should include a combination of variables and strings concatenated together using the plus symbol +.

Check to see if your program is correct by running this command:

javascripting verify scope.js


@michaelgrilo
Copy link
Contributor Author

Do we need to add a semi-colon so the line reads var me = 'i am a string';?

Using the copied code, it's taking me too long to logically come to a solution. I can only solve the problem with the following simplified code:

var aGlobalVariable = 'a global variable';

function iAmAString () {
  var me = 'i am a string'; 
  return me;
}

function aLocalVariable () {
   var local = 'a local variable';
   return local;
  }

console.log(iAmAString() + ' constructed from ' + aLocalVariable() + ' & ' + aGlobalVariable);

I get that var local is assigned to the result of the function aLocalVariable(), as determined by return local;. However, without a return statement for the variable me, I'm not sure how to pull both variables out of iAmAString(). I don't understand what I need pass to iAmAString(); to log the proper output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants