* chore(learn): audit basic algorithm scripting * chore(learn): audit basic data structures * chore(learn): audit basic javascript * chore(learn): audit debugging * chore(learn): audit es6 * chore(learn): audit functional programming * chore(learn): audit intermidate algorithms * chore(learn): audit js projects * chore(learn): audit object oriented programming * chore(learn): audit regex * fix(learn): remove stray . * fix(learn): string to code * fix(learn): missed some * fix(learn): clarify strings Based on Randy's feedback, clarifies string instances where quotes were removed in favour of back ticks. * fix: apply suggestions - thanks Randy! :) Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: non-suggestion comments * chore(learn): remove comments from codes Removes the comments from the description and instruction code blocks to ensure that all relevant information is translatable. * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: revert crowdin fix * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-algorithm-scripting/mutations.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-destructuring-assignment-to-assign-variables-from-arrays.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * fix: Apply suggestions from code review Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * chore: change voice * fix: Christopher Nolan * fix: expressions would evaluate * fix: will -> would * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: to work to push * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-for-loops.md Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/object-oriented-programming/add-methods-after-inheritance.md Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> Co-authored-by: Shaun Hamilton <51722130+ShaunSHamilton@users.noreply.github.com> Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
87 lines
1.9 KiB
Markdown
87 lines
1.9 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244bf
|
|
title: Local Scope and Functions
|
|
challengeType: 1
|
|
videoUrl: 'https://scrimba.com/c/cd62NhM'
|
|
forumTopicId: 18227
|
|
dashedName: local-scope-and-functions
|
|
---
|
|
|
|
# --description--
|
|
|
|
Variables which are declared within a function, as well as the function parameters, have <dfn>local</dfn> scope. That means they are only visible within that function.
|
|
|
|
Here is a function `myTest` with a local variable called `loc`.
|
|
|
|
```js
|
|
function myTest() {
|
|
var loc = "foo";
|
|
console.log(loc);
|
|
}
|
|
myTest();
|
|
console.log(loc);
|
|
```
|
|
|
|
The `myTest()` function call will display the string `foo` in the console. The `console.log(loc)` line will throw an error, as `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 display `ReferenceError: myVar is not defined`, but this will not cause the tests to fail.
|
|
|
|
# --hints--
|
|
|
|
The code should not contain a global `myVar` variable.
|
|
|
|
```js
|
|
function declared() {
|
|
myVar;
|
|
}
|
|
assert.throws(declared, ReferenceError);
|
|
```
|
|
|
|
You should add a local `myVar` variable.
|
|
|
|
```js
|
|
assert(
|
|
/functionmyLocalScope\(\)\{.+(var|let|const)myVar[\s\S]*}/.test(
|
|
__helpers.removeWhiteSpace(code)
|
|
)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function myLocalScope() {
|
|
|
|
// 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);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function myLocalScope() {
|
|
|
|
// 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);
|
|
```
|