Skip to content

Commit

Permalink
Code samples formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bpesquet committed Jul 26, 2017
1 parent 37ebbcc commit 108a1af
Show file tree
Hide file tree
Showing 16 changed files with 437 additions and 427 deletions.
6 changes: 3 additions & 3 deletions manuscript/chapter02.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ The **scope** of a variable is the part of the program where the variable is vis
```js
let nb1 = 0;
{
nb1 = 1; // OK : nb1 is declared in the parent block
const nb2 = 0;
nb1 = 1; // OK : nb1 is declared in the parent block
const nb2 = 0;
}
console.log(nb1); // OK : nb1 is declared in the current block
console.log(nb2); // Error! nb2 is not visible here
Expand Down Expand Up @@ -165,7 +165,7 @@ const f = 100;
console.log("Variable f contains the value " + f);
```

JavaScript is extremely tolerant in terms of type conversion. However, sometimes conversion isn't possible. If a number fails to convert, you'll get the result `NaN` (*Not a Number*).
JavaScript is extremely tolerant in terms of type conversion. However, sometimes conversion isn't possible. If a number fails to convert, you'll get the result `NaN` (*Not a Number*).

```js
const g = "five" * 2;
Expand Down
102 changes: 52 additions & 50 deletions manuscript/chapter03.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Up until now, all the code in our programs has been executed chronologically. Le

```js
if (condition) {
// Code to run when the condition is true
// Code to run when the condition is true
}
```

Expand All @@ -20,10 +20,10 @@ if (condition) {

```js
if (condition) {
// Code to run when the condition is true
// Code to run when the condition is true
}
else {
// Code to run when the condition is false
// Code to run when the condition is false
}
```

Expand All @@ -34,14 +34,14 @@ else {
```js
switch (expression) {
case value1:
// Code to run when the expression matches value1
break;
// Code to run when the expression matches value1
break;
case value2:
// Code to run when the expression matches value2
break;
// Code to run when the expression matches value2
break;
...
default:
// Code to run when neither case matches
// Code to run when neither case matches
}
```

Expand All @@ -64,7 +64,7 @@ Here's how you translate the program to JavaScript.
```js
const number = Number(prompt("Enter a number:"));
if (number > 0) {
console.log(`${number} is positive`);
console.log(`${number} is positive`);
}
```

Expand All @@ -74,7 +74,7 @@ Conditional syntax looks like this:

```js
if (condition) {
// Code to run when the condition is true
// Code to run when the condition is true
}
```

Expand All @@ -94,12 +94,12 @@ Any expression producing a boolean value (either `true` or `false`) can be used

```js
if (true) {
// The condition for this if is always true
// This block of code will always be executed
// The condition for this if is always true
// This block of code will always be executed
}
if (false) {
// The condition for this if is always false
// This block of code will never be executed
// The condition for this if is always false
// This block of code will never be executed
}
```

Expand All @@ -123,7 +123,7 @@ Now let's modify the example code to replace `>` with `>=` and change the messag
```js
const number = Number(prompt("Enter a number:"));
if (number >= 0) {
console.log(`${number} is positive or zero`);
console.log(`${number} is positive or zero`);
}
```

Expand All @@ -140,10 +140,10 @@ Let's enrich our sample with different messages depending if the number's positi
```js
const number = Number(prompt("Enter a number:"));
if (number > 0) {
console.log(`${number} is positive`);
console.log(`${number} is positive`);
}
else {
console.log(`${number} is negative or zero`);
console.log(`${number} is negative or zero`);
}
```

Expand All @@ -153,10 +153,10 @@ The syntax for creating an alternative is to add an `else` keyword after an init

```js
if (condition) {
// Code to run when the condition is true
// Code to run when the condition is true
}
else {
// Code to run when the condition is false
// Code to run when the condition is false
}
```

Expand All @@ -169,13 +169,15 @@ Let's go next level and display a specific message if the entered number is zero
```js
const number = Number(prompt("Enter a number:"));
if (number > 0) {
console.log(`${number} is positive`);
} else { // number <= 0
if (number < 0) {
console.log(`${number} is nagative`);
} else { // number === 0
console.log(`${number} is zero`);
}
console.log(`${number} is positive`);
} else {
// number <= 0
if (number < 0) {
console.log(`${number} is nagative`);
} else {
// number === 0
console.log(`${number} is zero`);
}
}
```

Expand All @@ -194,11 +196,11 @@ A particular case happens when the only statement in a `else` block is an `if`.
```js
const number = Number(prompt("Enter a number:"));
if (number > 0) {
console.log(`${number} is positive`);
console.log(`${number} is positive`);
} else if (number < 0) {
console.log(`${number} is negative`);
console.log(`${number} is negative`);
} else {
console.log(`${number} is zero`);
console.log(`${number} is zero`);
}
```

Expand All @@ -214,7 +216,7 @@ Here's how you'd translate that same check into JS.

```js
if ((number >= 0) && (number <= 100)) {
console.log(`${number} is between 0 and 100, both included`);
console.log(`${number} is between 0 and 100, both included`);
}
```

Expand All @@ -239,7 +241,7 @@ Here it is, translated into JavaScript:

```js
if ((number < 0) || (number > 100)) {
console.log(`${number} is not in between 0 and 100`);
console.log(`${number} is not in between 0 and 100`);
}
```

Expand All @@ -258,7 +260,7 @@ There's another operator for when you know what you don't want: the not operator

```js
if (!(number > 100)) {
console.log(`${number} is less than or equal to 100`);
console.log(`${number} is less than or equal to 100`);
}
```

Expand All @@ -276,15 +278,15 @@ Let's write some code that helps people decide what to wear based on the weather
```js
const weather = prompt("What's the weather like?");
if (weather === "sunny") {
console.log("T-shirt time!");
console.log("T-shirt time!");
} else if (weather === "windy") {
console.log("Windbreaker life.");
console.log("Windbreaker life.");
} else if (weather === "rainy") {
console.log("Bring that umbrella!");
console.log("Bring that umbrella!");
} else if (weather === "snowy") {
console.log("Just stay inside!");
console.log("Just stay inside!");
} else {
console.log("Not a valid weather type");
console.log("Not a valid weather type");
}
```

Expand All @@ -293,19 +295,19 @@ When a program should trigger a block from several operations depending on the v
```js
const weather = prompt("What's the weather like?");
switch (weather) {
case "sunny":
case "sunny":
console.log("T-shirt time!");
break;
case "windy":
case "windy":
console.log("Windbreaker life.");
break;
case "rainy":
case "rainy":
console.log("Bring that umbrella!");
break;
case "snowy":
case "snowy":
console.log("Winter is coming! Just stay inside!");
break;
default:
default:
console.log("Not a valid weather type");
}
```
Expand All @@ -316,15 +318,15 @@ The `switch` statement kicks off the execution of one code block among many. Onl

```js
switch (expression) {
case value1:
case value1:
// Code to run when the expression matches value1
break;
case value2:
case value2:
// Code to run when the expression matches value2
break;
...
default:
// Code to run when neither case matches
// ...
default:
// Code to run when neither case matches
}
```

Expand All @@ -335,10 +337,10 @@ Adding a `break;` in each block is important so you get out of the switch statem
```js
const x = "abc";
switch (x) {
case "abc":
case "abc":
console.log("x = abc");
// break omitted: the next block is also run!
case "def":
// break omitted: the next block is also run!
case "def":
console.log("x = def");
break;
}
Expand Down
34 changes: 17 additions & 17 deletions manuscript/chapter04.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ In this chapter, we'll look at how to execute code on a repeating basis.
```js
// While loop
while (condition) {
// Code to run while the condition is true
// Code to run while the condition is true
}

// For loop
for (initialization; condition; final expression) {
// code to run while the condition is true
// code to run while the condition is true
}
```

Expand Down Expand Up @@ -55,8 +55,8 @@ Here's a sample program written with a `while` loop.
```js
let number = 1;
while (number <= 5) {
console.log(number);
number++;
console.log(number);
number++;
}
```

Expand All @@ -70,7 +70,7 @@ You'll use the following syntax to write a `while` loop.

```js
while (condition) {
// Code to run while the condition is true
// Code to run while the condition is true
}
```

Expand All @@ -93,7 +93,7 @@ Here's the same program as above written instead with a `for` loop.
```js
let number;
for (number = 1; number <= 5; number++) {
console.log(number);
console.log(number);
}
```

Expand All @@ -105,7 +105,7 @@ Here's the `for` loop syntax.

```js
for (initialization; condition; final expression) {
// code to run while the condition is true
// code to run while the condition is true
}
```

Expand All @@ -123,9 +123,9 @@ The variable used during initialization, condition, and the final expression of

```js
for (let i = 1; i <= 5; i++) {
console.log(i);
console.log(i); // OK
}
// The i variable is not visible here
console.log(i); // Error: the i variable is not visible here
```

## Common mistakes
Expand All @@ -137,8 +137,8 @@ The main risk with `while` loops is producing an **infinite loop**, meaning the
```js
let number = 1;
while (number <= 5) {
console.log(number);
// The number variable is not updated: the loop condition stays true forever
console.log(number);
// The number variable is never updated: the loop condition stays true forever
}
```

Expand All @@ -150,8 +150,8 @@ Imagine that you accidentally modify the loop counter in the loop body, just lik

```js
for (let i = 1; i <= 5; i++) {
console.log(i);
i++; // The i variable is updated in the loop body
console.log(i);
i++; // The i variable is updated in the loop body
}
```

Expand All @@ -168,7 +168,7 @@ Each time the loop runs, the counter variable is incremented *twice*: once in th
```js
let letter = "";
while (letter !== "X") {
letter = prompt("Type a letter or X to exit:");
letter = prompt("Type a letter or X to exit:");
}
```

Expand All @@ -192,9 +192,9 @@ Check the following program that shows even number (divisibles by 2) betwen 1 an

```js
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
console.log(`${i} is even`);
}
if (i % 2 === 0) {
console.log(`${i} is even`);
}
}
```

Expand Down
Loading

0 comments on commit 108a1af

Please sign in to comment.