Published
Edited
Dec 4, 2020
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.
Learn more