Platform
Solutions
Resources
Pricing
Sign in
Sign up
Anjana Vakil
Engineer & Educator | Observable Ambassador | Karaoke Enthusiast
Workspace
Fork
Published
Functional JavaScript First Steps
By
Anjana Vakil
Edited
Dec 4, 2020
42 forks
39 stars
Functional JavaScript First Steps
Functional JavaScript First Steps
What is functional programming?
Exercise: Pure Functions
Staying out of the loop with recursion
Exercise: Iteration vs. Recursion
Flying first class with higher-order functions
Exercise: Filter, Map, Reduce
Getting closure
Exercise: Closures & Currying
It's functions all the way down
Exercise: Function Composition
Immutability
Exercise: Avoiding mutation
Recap & takeaways
Next steps
Insert cell
Insert cell
Insert cell
Insert cell
function
getDate
(
)
{
return
new
Date
(
)
.
toDateString
(
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
getWorkshopDate
(
)
{
return
new
Date
(
2020
,
11
,
4
)
.
toDateString
(
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
toHex
(
n
)
{
let
hex
=
n
.
toString
(
16
)
;
return
hex
.
padStart
(
2
,
'0'
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
rgbToHex
(
R
,
G
,
B
)
{
return
'#'
+
[
toHex
(
R
)
,
toHex
(
G
)
,
toHex
(
B
)
]
.
join
(
''
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
setColor
(
R
,
G
,
B
)
{
const
hex
=
rgbToHex
(
R
,
G
,
B
)
;
const
colorMe
=
document
.
getElementById
(
'color-me'
)
;
colorMe
.
setAttribute
(
'style'
,
'color: '
+
hex
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
async
function
readJsonFile
(
filename
)
{
const
file
=
await
fetch
(
'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson'
)
;
return
await
file
.
json
(
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
writeJsonString
(
object
)
{
return
JSON
.
stringify
(
object
,
null
,
2
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
exclusiveOr
(
A
,
B
)
{
return
(
A
||
B
)
&&
!
(
A
&&
B
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
computeTruthTable
(
operator
)
{
const
truthValues
=
[
true
,
false
]
;
const
table
=
[
]
;
for
(
const
A
of
truthValues
)
{
for
(
const
B
of
truthValues
)
{
const
value
=
operator
(
A
,
B
)
;
table
.
push
(
{
A
,
B
,
value
}
)
;
}
}
return
table
;
}
Insert cell
Insert cell
Insert cell
Insert cell
function
showTruthTable
(
operator
)
{
console
.
table
(
computeTruthTable
(
operator
)
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
One platform
to build and deploy the best data apps
Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Try it for free
Learn more
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getDate
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getDateAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getWorkshopDate
Show 5 comments
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getWorkshopDateAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
toHex
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
toHexAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rgbToHex
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
rgbToHexAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
setColor
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
setColorAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
readJsonFile
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
readJsonFileAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
writeJsonString
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
writeJsonStringAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
exclusiveOr
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
exclusiveOrAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
computeTruthTable
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
computeTruthTableAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
showTruthTable
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
showTruthTableAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
evaluate
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
getAnswer
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
pureOrNot
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML