Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Trevor Johnson
Trevor Johnson
14,427 Points

JS Help With Local Scope & Functions

I am trying to do this code challenge on Free Code Camp, and it seems to me like I am doing everything right but it isn't working. Any help is appreciated

Here are the directions:

Declare a local variable myVar inside myLocalScope. Run the tests and then follow the instructions commented out in the editor.

function myLocalScope() {
  var myVar = 'use strict';
  console.log(myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope

console.log(myVar);

// Now remove the console log line to pass the test
Trevor Johnson
Trevor Johnson
14,427 Points

I get that it is trying to show me that a local variable can't be called outside of a function. Nothing changes though when I remove the console.log(myVar) which tries to call the local variable that doesn't exist globally.

1 Answer

andren
andren
28,520 Points

If you removed the second console call then you don't seem to be doing anything wrong. I decided to try the test myself and using your code with the console call removed:

function myLocalScope() {
  var myVar = 'use strict';
  console.log(myVar);
}
myLocalScope();

It did seem to pass the test fine, maybe it was just a temporary bug?

Trevor Johnson
Trevor Johnson
14,427 Points

It seemed to be a bug. It let me pass this time.