Local scope and functions lesson in JavaScript

My code on local scope and functions is not getting passed. Don’t know what else to do please.

Your code so far


function myLocalScope() {
var myVar = 8;
console.log(myVar);
}
myLocalScope();

console.log(myVar);

Your browser information:

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

Challenge: Local Scope and Functions

Link to the challenge:

Hi and welcome to the forum!

function myLocalScope() {
  'use strict';

  // Only change code below this line

  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

You made changes to the starter code above the comment // Only change code below this line. When you change code that you were asked not to change, this can cause the test suite to fail.

1 Like

Thanks… Noted…
How can I get the starter code back?

There should be a ‘Reset Al Code’ button near the ‘Run the Tests’ button:

1 Like

It still does not pass. What is being output on the dev console is as follows:

// running tests You should add a local

myVar

variable. // tests completed // console output ReferenceError: myVar is not defined ReferenceError: myVar is not defined

Once you reset the code, did you add back the definition of myVar? What’s your current code?

How can I add my current code here, help.

When you enter a code block into a forum post, you precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

function myLocalScope() {
  'use strict';

  // Only change code below this line
  var MyVar = 5;
  console.log('inside myLocalScope', myVar);
}
myLocalScope();

// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);

Test error:

You should add a local myVar variable.

Your code:

  var MyVar = 5;

Those sneaky little differences will get you every time

1 Like

Don’t know what else to do.
MyVar is declared within the function. Think will skip this lesson.

Your capitalization is wrong.

1 Like

Yessssss… All thanks to you. Just made the correction and got it passed. You are the boss!

1 Like