fix(curriculum): separate unreachable code and scope for pyramid project (#54920)

Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com>
Co-authored-by: Lasse Jørgensen <28780271+lasjorg@users.noreply.github.com>
This commit is contained in:
Supravisor
2024-06-04 12:20:44 +12:00
committed by GitHub
parent fe2eafaf53
commit 26d5eaad65
4 changed files with 65 additions and 12 deletions

View File

@@ -7,7 +7,7 @@ dashedName: step-55
# --description--
Variables can also be declared inside a function. These variables are considered to be in the <dfn>local scope</dfn>, or <dfn>block scope</dfn>. A variable declared inside a function can only be used inside that function. If you try to access it outside of the function, you will either get `undefined` or an error.
Variables can also be declared inside a function. These variables are considered to be in the <dfn>local scope</dfn>, or <dfn>block scope</dfn>. A variable declared inside a function can only be used inside that function. If you try to access it outside of the function, you get a reference error.
To see this in action, use `const` to declare a `test` variable in your `padRow` function. Initialise it with the value `"Testing"`.
@@ -27,6 +27,13 @@ You should initialise `test` with the value `"Testing"`. Don't forget the semi-c
assert.match(padRow.toString(), /var\s+test\s*=\s*('|")Testing\1;/)
```
Your `test` variable should come before your `return` keyword.
```js
const str = padRow.toString();
assert.isBelow(str.indexOf("test"), str.indexOf("return"));
```
# --seed--
## --seed-contents--
@@ -38,8 +45,8 @@ const rows = [];
--fcc-editable-region--
function padRow(name) {
return character + name;
return character + name;
}
--fcc-editable-region--

View File

@@ -7,17 +7,31 @@ dashedName: step-56
# --description--
There is a bit of a problem, here. Your `test` declaration is currently after your `return` statement. An important thing to know about the `return` keyword is that it does not just define a value to be returned from your function, it also stops the execution of your function code. This means that any code after a `return` statement will not run.
Values returned out of a function are used by calling the function. You can use the function call directly as the value it returns, or capture the returned value in a variable. This way, you can use the value assigned to a locally scoped variable, outside the function it was created in.
```js
function getName() {
const name = "Camper cat";
return name;
}
console.log(getName()); // "Camper cat"
const capturedReturnValue = getName();
console.log(capturedReturnValue); // "Camper cat"
console.log(name); // reference error
```
To use your `"Testing"` value, return it out of the `padRow` function by updating your `return` statement to return only the `test` variable.
Move your `test` initialization to the line above your `return` statement.
# --hints--
Your `test` variable should come before your `return` keyword.
Your `padRow` function should return the `test` variable.
```js
const str = padRow.toString();
assert.isBelow(str.indexOf("test"), str.indexOf("return"));
assert.equal(padRow("Naomi"), "Testing");
```
# --seed--
@@ -31,9 +45,8 @@ const rows = [];
--fcc-editable-region--
function padRow(name) {
const test = "Testing";
return character + name;
const test = "Testing";
}
--fcc-editable-region--

View File

@@ -7,9 +7,13 @@ dashedName: step-57
# --description--
If you try to add a `console.log(test)` call below your `padRow` function, you would see an error. This is because `test` is defined in the local scope, meaning you cannot access it in the global scope (outside of the `padRow` function).
Below the `return` statement, log the string `"This works!"` to the console.
Returning a value from a function brings that value into the scope where the function was called. To bring your `"Testing"` value from the `padRow` function into the global scope, update your `return` statement to return only the `test` variable.
After doing that, you will see that the string `"This works!"` does not display in the console, and the `console.log("This works!")` line is greyed out.
Copy the console log and paste it above the `return` statement. Now, the string `"This works!"` should appear in the console.
An important thing to know about the `return` keyword is that it does not just define a value to be returned from your function, it also stops the execution of your code inside a function or a block statement. This means any code after a `return` statement will not run.
# --hints--
@@ -19,6 +23,18 @@ Your `padRow` function should return the `test` variable.
assert.equal(padRow("Naomi"), "Testing");
```
Your first `console.log` should come after your `return` keyword.
```js
assert.match(padRow.toString(), /return\s+test;\s+console.log\s*\(\s*('|"|`)This\s+works!\1\);/);
```
Your second `console.log` should come before your `return` keyword.
```js
assert.match(padRow.toString(), /console.log\s*\(\s*('|"|`)This\s+works!\1\);\s+return\s+test;/);
```
# --seed--
## --seed-contents--
@@ -31,7 +47,9 @@ const rows = [];
--fcc-editable-region--
function padRow(name) {
const test = "Testing";
return character + name;
return test;
}
--fcc-editable-region--

View File

@@ -11,6 +11,7 @@ Now your `call` variable has the value `"Testing"`. But your function is no long
Remove the `name` parameter from your function declaration, then remove your `"CamperChan"` string from the `padRow` call.
Also, remove both `console.log` from the `padRow` function.
# --hints--
Your `padRow` function should not have a `name` parameter.
@@ -31,6 +32,18 @@ You should still call your `padRow` function.
assert.lengthOf(code.match(/padRow\(\)/g), 2);
```
You should not have a `console.log` before your `return` keyword.
```js
assert.notMatch(padRow.toString(), /console.log\s*\(\s*('|"|`)This\s+works!\1\);\s+return\s+test;/);
```
You should not have a `console.log` after your `return` keyword.
```js
assert.notMatch(padRow.toString(), /return\s+test;\s+console.log\s*\(\s*('|"|`)This\s+works!\1\);/);
```
# --seed--
## --seed-contents--
@@ -43,7 +56,9 @@ const rows = [];
--fcc-editable-region--
function padRow(name) {
const test = "Testing";
console.log("This works!");
return test;
console.log("This works!");
}
const call = padRow("CamperChan");
console.log(call);