Published
Edited
Dec 2, 2019
Insert cell
Insert cell
Insert cell
answer = input.reduce((fuel, mass) => {
fuel += Math.floor(mass / 3) - 2;
return fuel;
}, 0)
Insert cell
Insert cell
input.reduce((fuel, mass) => {
fuel += calcTotalFuel(mass);
return fuel;
}, 0)
Insert cell
function calcTotalFuel(mass) {
let fuel = 0;
while(mass > 0) {
mass = Math.floor(mass / 3) - 2;
// Protect against the negative numbers on the final calc
if(mass > 0) {
fuel += mass;
}
}
return fuel;
}
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