2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
56533eb9ac21ba0edf2244bf | Local Scope and Functions | 1 | https://scrimba.com/c/cd62NhM | 18227 |
Description
myTest
with a local variable called loc
.
function myTest() {
var loc = "foo";
console.log(loc);
}
myTest(); // logs "foo"
console.log(loc); // loc is not defined
loc
is not defined outside of the function.
Instructions
The editor has two console.log
s to help you see what is happening. Check the console as you code to see how it changes. Declare a local variable myVar
inside myLocalScope
and run the tests.
Note: The console will still have 'ReferenceError: myVar is not defined', but this will not cause the tests to fail.
Tests
tests:
- text: The code should not contain a global <code>myVar</code> variable.
testString: |
function declared(){
myVar;
}
assert.throws(declared, ReferenceError);
- text: You should add a local <code>myVar</code> variable.
testString: assert(/functionmyLocalScope\(\)\{.+(var|let|const)myVar.*}/s.test(code.replace(/\s/g, '')));
Challenge Seed
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);
Solution
function myLocalScope() {
'use strict';
// Only change code below this line
var myVar;
console.log('inside myLocalScope', myVar);
}
myLocalScope();
// Run and check the console
// myVar is not defined outside of myLocalScope
console.log('outside myLocalScope', myVar);