◐ Shell
reader mode source ↗
Skip to content
Open
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
14 changes: 7 additions & 7 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,4 +1,4 @@
The answer: `1`.

```js run
let i = 3;
Expand All @@ -8,18 +8,18 @@ while (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 );
```
14 changes: 7 additions & 7 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 );
```
4 changes: 1 addition & 3 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,5 +1,3 @@


```js run demo
for (let i = 2; i <= 10; i++) {
if (i % 2 == 0) {
Expand All @@ -8,4 +6,4 @@ for (let i = 2; i <= 10; i++) {
}
```

We use the "modulo" operator `%` to get the remainder and check for the evenness here.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@


```js run
let i = 0;
while (i < 3) {
alert( `number ${i}!` );
i++;
}
```

7 changes: 3 additions & 4 deletions 1-js/02-first-steps/13-while-for/5-replace-for-while/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ importance: 5

---

# Replace "for" with "while"

Rewrite the code changing the `for` loop to `while` without altering its behavior (the output should stay same).

```js run
for (let i = 0; i < 3; i++) {
alert( `number ${i}!` );
}
```

Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@

```js run demo
let num;

do {
num = prompt("Enter a number greater than 100?", 0);
} while (num <= 100 && num);
```

The loop `do..while` repeats while both checks are truthy:

1. The check for `num <= 100` -- that is, the entered value is still not greater than `100`.
2. The check `&& num` is false when `num` is `null` or an empty string. Then the `while` loop stops too.

P.S. If `num` is `null` then `num <= 100` is `true`, so without the 2nd check the loop wouldn't stop if the user clicks CANCEL. Both checks are required.
Original file line number Diff line number Diff line change
@@ -2,12 +2,10 @@ importance: 5

---

# Repeat until the input is correct

Write a loop which prompts for a number greater than `100`. If the visitor enters another number -- ask them to input again.

The loop must ask for a number until either the visitor enters a number greater than `100` or cancels the input/enters an empty line.

Here we can assume that the visitor only inputs numbers. There's no need to implement a special handling for a non-numeric input in this task.

[demo]
26 changes: 13 additions & 13 deletions 1-js/02-first-steps/13-while-for/7-list-primes/solution.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
There are many algorithms for this task.

Let's use a nested loop:

```js
For each i in the interval {
check if i has a divisor from 1..i
if yes => the value is not a prime
if no => the value is a prime, show it
}
```

The code using a label:

```js run
let n = 10;

nextPrime:
for (let i = 2; i <= n; i++) { // for each i...

for (let j = 2; j < i; j++) { // look for a divisor..
if (i % j == 0) continue nextPrime; // not a prime, go next i
}

alert( i ); // a prime
}
```

There's a lot of space to optimize it. For instance, we could look for the divisors from `2` to square root of `i`. But anyway, if we want to be really efficient for large intervals, we need to change the approach and rely on advanced maths and complex algorithms like [Quadratic sieve](https://en.wikipedia.org/wiki/Quadratic_sieve), [General number field sieve](https://en.wikipedia.org/wiki/General_number_field_sieve) etc.
14 changes: 6 additions & 8 deletions 1-js/02-first-steps/13-while-for/7-list-primes/task.md
Original file line number Diff line number Diff line change
@@ -2,16 +2,14 @@ importance: 3

---

# Output prime numbers

An integer number greater than `1` is called a [prime](https://en.wikipedia.org/wiki/Prime_number) if it cannot be divided without a remainder by anything except `1` and itself.

In other words, `n > 1` is a prime if it can't be evenly divided by anything except `1` and `n`.

For example, `5` is a prime, because it cannot be divided without a remainder by `2`, `3` and `4`.

**Write the code which outputs prime numbers in the interval from `2` to `n`.**

For `n = 10` the result will be `2,3,5,7`.

P.S. The code should work for any `n`, not be hard-tuned for any fixed value.
Loading
Toggle all file notes Toggle all file annotations