Platform
Resources
Pricing
Sign in
Get started
Caroline Mastel
Workspace
Fork
Published
By
Caroline Mastel
Edited
Feb 8, 2020
Fork of
Lesson 4: if, for, while, and some functions
Insert cell
Insert cell
Insert cell
Insert cell
// DECLARAING A FUNCTION
function
myFunctionThatCalculatesASum
(
parameter1
,
parameter2
)
{
// CODE BLOCK: do stuff in here
var
sumOfMyParamters
=
parameter1
+
parameter2
return
sumOfMyParamters
// <-- this is outputted from the function
}
Insert cell
// RUNNNING THE FUNCTION
theOutput
=
myFunctionThatCalculatesASum
(
true
,
true
)
Insert cell
{
function
sum
(
a
,
b
)
{
return
a
+
b
}
function
product
(
a
,
b
)
{
return
a
*
b
}
var
onePlusTwo
=
sum
(
1
,
2
)
var
threePlusFour
=
sum
(
3
,
4
)
var
productOfTwoVariables
=
product
(
onePlusTwo
,
threePlusFour
)
return
productOfTwoVariables
// <-- ignore that return. it's to make the notebook work
}
Insert cell
Insert cell
{
var
bob
=
function
(
)
{
return
"Hi, I'm in Bob!"
}
var
joe
=
bob
(
)
+
" And now I'm called by Joe!"
return
joe
}
Insert cell
Insert cell
function
checkDay
(
today
)
{
if
(
today
===
"Sunday"
||
today
===
"Saturday"
)
{
return
"WEEKEND"
}
else
{
return
"WEEKDAY"
}
}
Insert cell
checkDay
(
"Sunday"
)
Insert cell
Insert cell
{
// COUNTING TO TEN
var
count
=
0
while
(
count
!=
10
)
{
count
=
count
+
1
;
}
return
count
;
}
Insert cell
Insert cell
Insert cell
Insert cell
{
// COUNTING TO TEN
var
count
=
0
for
(
var
i
=
0
;
i
<
10
;
i
++
)
{
count
=
count
+
1
count
=
count
+
1
count
=
count
+
1
count
=
count
+
1
count
=
count
+
2
count
=
count
+
1
count
=
count
+
1
count
=
count
+
1
count
=
count
+
1
}
return
count
}
Insert cell
Insert cell
Insert cell
function
testable
(
isGood
)
{
if
(
isGood
===
true
)
return
"yes"
return
"no"
}
Insert cell
test1
=
testable
(
true
)
===
"yes"
Insert cell
test2
=
testable
(
false
)
===
"no"
Insert cell
test3
=
testable
(
1
)
===
"yes"
Insert cell
Insert cell
Insert cell
function
sleepIn
(
weekday
,
vacation
)
{
if
(
weekday
==
false
||
vacation
==
true
)
{
return
true
}
else
{
return
false
;
}
}
Insert cell
Insert cell
Insert cell
function
hasTeen
(
num1
,
num2
,
num3
)
{
if
(
num1
>=
13
&&
num1
<=
19
)
{
return
true
}
else
if
(
num2
>=
13
&&
num2
<=
19
)
{
return
true
}
else
if
(
num3
>=
13
&&
num3
<=
19
)
{
return
true
}
else
{
return
false
}
}
Insert cell
Insert cell
Insert cell
function
front3
(
str
)
{
str
=
str
.
substring
(
0
,
3
)
var
newstr
=
""
for
(
var
i
=
0
;
i
<
3
;
i
++
)
{
newstr
=
newstr
+
str
}
return
newstr
}
Insert cell
Insert cell
Insert cell
function
findPrimes
(
input
)
{
{
var
store
=
[
]
,
i
,
j
,
primes
=
[
]
;
for
(
i
=
2
;
i
<=
max
;
++
i
)
{
if
(
!
store
[
i
]
)
{
primes
.
push
(
i
)
;
for
(
j
=
i
<<
1
;
j
<=
max
;
j
+=
i
)
{
store
[
j
]
=
true
;
}
}
}
return
primes
;
}
;
}
Insert cell
Insert cell
Insert cell
Insert cell
Purpose-built for displays of data
Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Try it for free
Learn more
Compare fork
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
myFunctionThatCalculatesASum
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
theOutput
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
checkDay
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
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
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
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
testable
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test1
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test2
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
test3
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
sleepIn
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
hasTeen
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
front3
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
findPrimes
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
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML