◐ Shell
reader mode source ↗
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
JavaScript-code:

```js demo run
let name = prompt("What is your name?", "");
alert(name);
```

The full page:
@@ -15,8 +15,8 @@ The full page:
<script>
'use strict';

let name = prompt("What is your name?", "");
alert(name);
</script>

</body>
Expand Down
16 changes: 8 additions & 8 deletions 1-js/02-first-steps/13-while-for/1-loop-last-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
The answer: `1`.

```js run
let i = 3;

while (i) {
alert( i-- );
}
```

Every loop iteration decreases `i` by `1`. The check `while(i)` stops the loop when `i = 0`.

Hence, the steps of the loop form the following sequence ("loop unrolled"):

```js
let i = 3;

alert(i--); // shows 3, decreases i to 2

alert(i--) // shows 2, decreases i to 1

alert(i--) // shows 1, decreases i to 0

// done, while(i) check stops the loop
```
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/13-while-for/1-loop-last-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# Last loop value

What is the last value alerted by this code? Why?

```js
let i = 3;
Expand Down
26 changes: 13 additions & 13 deletions 1-js/02-first-steps/13-while-for/2-which-value-while/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ importance: 4

---

# Which values does the while loop show?

For every loop iteration, write down which value it outputs and then compare it with the solution.

Both loops `alert` the same values, or not?

1. The prefix form `++i`:

```js
let i = 0;
while (++i < 5) alert( i );
```
2. The postfix form `i++`

```js
let i = 0;
while (i++ < 5) alert( i );
```
16 changes: 8 additions & 8 deletions 1-js/02-first-steps/13-while-for/3-which-value-for/solution.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
**The answer: from `0` to `4` in both cases.**

```js run
for (let i = 0; i < 5; ++i) alert( i );

for (let i = 0; i < 5; i++) alert( i );
```

That can be easily deducted from the algorithm of `for`:

1. Execute once `i = 0` before everything (begin).
2. Check the condition `i < 5`
3. If `true` -- execute the loop body `alert(i)`, and then `i++`

The increment `i++` is separated from the condition check (2). That's just another statement.

The value returned by the increment is not used here, so there's no difference between `i++` and `++i`.
22 changes: 11 additions & 11 deletions 1-js/02-first-steps/13-while-for/3-which-value-for/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ importance: 4

---

# Which values get shown by the "for" loop?

For each loop write down which values it is going to show. Then compare with the answer.

Both loops `alert` same values or not?

1. The postfix form:

```js
for (let i = 0; i < 5; i++) alert( i );
```
2. The prefix form:

```js
for (let i = 0; i < 5; ++i) alert( i );
```
6 changes: 2 additions & 4 deletions 1-js/02-first-steps/13-while-for/4-for-even/solution.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@


```js run demo
for (let i = 2; i <= 10; i++) {
if (i % 2 == 0) {
alert( i );
}
}
```

We use the "modulo" operator `%` to get the remainder and check for the evenness here.
Loading
Toggle all file notes Toggle all file annotations