/
description.en.yml
81 lines (53 loc) · 2.74 KB
/
description.en.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
name: Increment and decrement
theory: |
JavaScript has adopted two operators from the C language: **increment** `++` and **decrement** `--`, which are commonly used with loops. These unary operators add or subtract a one from a number in a variable:
```javascript
let i = 0;
i++; // 0
i++; // 1
i--; // 2
i--; // 1
```
They can be used as a prefix and postfix:
```javascript
let i = 0;
++i; // 1
++i; // 2
--i; // 1
--i; // 0
```
It seems to be no difference between the postfix and prefix usages. And that's where it gets complicated.
Unlike other operators, which have no side-effects and simply return a new value, increment and decrement not only return a value but also **change** it.
If you use a prefix, the variable is changed first, and then it's returned.
If you use a postfix, it's the opposite: the variable is returned first, and then its value is changed
The rule works exactly the same for both incrementing and decrementing. For simplicity's sake, we'll only look at increments for now:
```javascript
let x = 5;
console.log(++x); // => 6
console.log(x); // => 6
console.log(x++); // => 6
console.log(x); // => 7
```
What's going on?
1. `++x` has been printed. It's a prefix increment, so initially, the value increased by 1, and then the result was returned and printed
2. Since the value changed, `console.log(x)` printed 6
3. Now print `x++`. It's a postfix increment, so it returns the value before it increments by 1
4. Since the value changed, `console.log(x)` printed 7
---
It gets worse when increments are put into other operations: `x = i++ - 7 + --h`. It's almost impossible to understand such code and writing it should be considered a serious crime.
For example, the linter (the program that checks the code) in JavaScript immediately starts to complain when it sees an increment or decrement.
Usage guidelines:
* Never mix operations/functions without side effects with operations/functions with side effects within a single expression
* Use increment and decrement only if there is no difference between the prefix and postfix version. It should be separate from other expressions and on a separate line of code
instructions: |
Write a function, `makeItFunny()`, which returns a copy of a string passed to it, where each nth element is uppercase. n also needs to be passed to the function.
To find each nth element, you will need to find the remainder from division `%`. Think about how you can use it.
```javascript
const text = 'I never look back';
// Every 3rd element
makeItFunny(text, 3); // 'I NevEr LooK bAck'
```
tips:
- |
[Remainder operator (%)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder)