-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path2-condition.js
More file actions
36 lines (30 loc) · 797 Bytes
/
2-condition.js
File metadata and controls
36 lines (30 loc) · 797 Bytes
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
'use strict';
// Declarative Style & Pure Functions
// Conditional expression instead of Conditional statement
const person = {
name: 'Marcus Aurelius',
born: 121,
city: 'Roma',
};
// Imperative
// Step-by-step instructions, mutable variables
{
let output = `${person.born} `;
if (person.born < 0) {
output += 'BC';
} else {
output += 'AD';
}
output = `${person.name} was born in ${output}`;
console.dir(output);
}
// Functional
// Declarative expressions, pure functions
{
const era = (year) => (year < 0 ? 'BC' : 'AD');
const formatBirthYear = (year) => `${year} ${era(year)}`;
const formatPersonInfo = (name, born) =>
`${name} was born in ${formatBirthYear(born)}`;
const output = formatPersonInfo(person.name, person.born);
console.dir(output);
}