Public
Edited
Mar 10, 2023
Insert cell
Insert cell
Insert cell
fizzBuzz = (length) => {
let string = "";

for (let i = 0; i <= length; i++) {
if (i % 3 == 0 && i % 5 == 0) {
string += "FizzBuzz \n";
} else if (i % 3 == 0) {
string += "Fizz \n";
} else if (i % 5 == 0) {
string += "Buzz \n";
}
string += `${i} \n`;
}

return string;
}
Insert cell
fizzBuzz(100)
Insert cell
Insert cell
isPrime = (number) => {
if (number == 0) return false; // 0 is not prime
if (number == 1) return false; // 1 is not prime
if (number == 2) return true; // 2, oddly enough, is prime
for (let i = 2; i <= number - 1; i++) {
//skip numbers that are the exceptional factors to prime numbers
if (number % i == 0) {
//check to see if there is a remainder
return false; // return false on additonal factors to 1
}
}
return true;
}
Insert cell
isPrime2 = (number) => {
for (let i = 2; i < number; i++) if (number % i === 0) return false;
return number > 1;
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
arrayFirst100Primes = () => {
let currentNum = 0;
let primes = [];

while (primes.length < 100) {
if (isPrime(currentNum)) primes.push(currentNum);
currentNum++;
}

return primes;
}
Insert cell
Insert cell
fibonacci(30)
Insert cell
fibonacci = (length) => {
let array = [];
let n1 = 0;
let n2 = 1;
let newNum;
for (let i = 0; i <= length; i++) {
let newNum = n1 + n2;
n1 = n2;
n2 = newNum;
array.push(n1);
}
return array;
}
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