FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FunctionalProgramming/JavaScript/2-condition.js at master · HowProgrammingWorks/FunctionalProgramming · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
HowProgrammingWorks
/
FunctionalProgramming
Public
Notifications
You must be signed in to change notification settings
Fork
6
Star
11
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
FunctionalProgramming
/
JavaScript
/
2-condition.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (30 loc) · 797 Bytes
Breadcrumbs
FunctionalProgramming
/
JavaScript
/
2-condition.js
Copy path
File metadata and controls
36 lines (30 loc) · 797 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
)
;
}
Back
|
FazBrowse Home
|
New Git URL