fix(guide): Fix all frontmatter

This commit is contained in:
Bouncey
2018-10-19 13:53:51 +01:00
committed by Stuart Taylor
parent 569bd7c3a7
commit 6d511c558a
146 changed files with 926 additions and 1469 deletions

View File

@ -1,4 +1,6 @@
---
title: Replacing If Else Chains with Switch
---
## Replacing If Else Chains with Switch
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
@ -26,8 +28,8 @@ function chainToSwitch(val) {
answer = "Ate Nine";
break;
}
// Only change code above this line
return answer;
// Only change code above this line
return answer;
}
// Change this value to test
chainToSwitch(7);
@ -36,9 +38,9 @@ chainToSwitch(7);
We need to change the chained ```if/else if``` statements into a ```switch``` statement.
Heres a solution:
Now, we need to comment (```//``` - select all lines and ```ctrl+/```) all chained ```if/else if``` statements:
```javascript
// if (val === "bob") {
// answer = "Marley";
@ -52,16 +54,16 @@ We need to change the chained ```if/else if``` statements into a ```switch``` st
// answer = "Ate Nine";
// }
```
Next, we need to create simple ```switch``` statement:
```javascript
switch(val) {
}
}
```
and add in this ```switch``` statement ```case``` - for all ```if/else if``` statement (just copy it from our commented code above):
```javascript
switch(val) {
case "bob":
@ -80,13 +82,13 @@ We need to change the chained ```if/else if``` statements into a ```switch``` st
answer = "Ate Nine";
break;
}
```
```
Dont forget to use ```break``` in each ```case```!
Now, we can delete commented code with ```if/else if``` statement above.
Heres a full solution:
```javascript
function chainToSwitch(val) {
var answer = "";
@ -107,9 +109,9 @@ We need to change the chained ```if/else if``` statements into a ```switch``` st
case 7:
answer = "Ate Nine";
break;
}
// Only change code above this line
return answer;
}
// Only change code above this line
return answer;
}
// Change this value to test
chainToSwitch(7);

View File

@ -1,4 +1,6 @@
---
title: Return Early Pattern for Functions
---
## Return Early Pattern for Functions
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
@ -9,7 +11,7 @@ Heres a setup:
// Setup
function abTest(a, b) {
// Only change code below this line
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
}
@ -27,7 +29,7 @@ We add in body of function simple ```if``` statement, which, under the condition
}
```
Now, if ```a``` or ```b``` are less than ```0``` - function exit with a value of ```undefined```, in other cases -
Now, if ```a``` or ```b``` are less than ```0``` - function exit with a value of ```undefined```, in other cases -
```javascript
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));
@ -35,7 +37,7 @@ Now, if ```a``` or ```b``` are less than ```0``` - function exit with a value of
```
Heres a full solution:
```javascript
// Setup
function abTest(a, b) {
@ -43,7 +45,7 @@ function abTest(a, b) {
if (a < 0 || b < 0) {
return undefined;
}
// Only change code above this line
return Math.round(Math.pow(Math.sqrt(a) + Math.sqrt(b), 2));