Platform
Resources
Pricing
Sign in
Get started
Anjana Vakil
Engineer & Educator | Observable Ambassador | Karaoke Enthusiast
Workspace
Fork
Published
By
Anjana Vakil
Edited
Dec 4, 2020
Fork of
Exercise: Iteration vs. Recursion
•
4 forks
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
function
iterativeFactorial
(
n
)
{
let
product
=
1
;
while
(
n
>
0
)
{
product
*=
n
;
n
--
;
}
return
product
;
}
Insert cell
iterativeFactorial
(
3
)
// Try changing the input to test out the function
Insert cell
function
recursiveFactorial
(
n
)
{
if
(
n
===
0
)
return
1
;
return
n
*
recursiveFactorial
(
n
-
1
)
;
}
Insert cell
recursiveFactorial
(
3
)
// Try changing the input to test out the function
Insert cell
Insert cell
function
iterativeFibonacci
(
n
)
{
if
(
n
===
0
)
return
0
;
if
(
n
===
1
)
return
1
;
let
previous
=
0
;
let
current
=
1
;
for
(
let
i
=
n
;
i
>
1
;
i
--
)
{
let
next
=
previous
+
current
;
previous
=
current
;
current
=
next
;
}
return
current
;
}
Insert cell
Insert cell
function
recursiveFibonacci
(
n
)
{
if
(
n
===
0
)
return
0
;
if
(
n
===
1
)
return
1
;
return
recursiveFibonacci
(
n
-
2
)
+
recursiveFibonacci
(
n
-
1
)
;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
iterativeFibonacci
(
n
)
Insert cell
recursiveFibonacci
(
n
)
Insert cell
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
iterativeFactorial
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
recursiveFactorial
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
iterativeFibonacci
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
recursiveFibonacci
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
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
n
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
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
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML