curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68c497f3aaefc9fd9f1b0e25.md
For the final day of Space Week, you will be given the mass in kilograms (kg) of a payload you want to send to orbit. Determine the amount of fuel needed to send your payload to orbit using the following rules:
For example, given a payload mass of 50 kg, you would need 10 kg of fuel to lift it (payload / 5), which increases the total mass to 60 kg, which needs 12 kg to lift (2 additional kg), which increases the total mass to 62 kg, which needs 12.4 kg to lift - 0.4 additional kg - which is less 1 additional kg, so we stop here. The total mass to lift is 62.4 kg, 50 of which is the initial payload and 12.4 of fuel.
launchFuel(50) should return 12.4.
assert.equal(launchFuel(50), 12.4);
launchFuel(500) should return 124.8.
assert.equal(launchFuel(500), 124.8);
launchFuel(243) should return 60.7.
assert.equal(launchFuel(243), 60.7);
launchFuel(11000) should return 2749.8.
assert.equal(launchFuel(11000), 2749.8);
launchFuel(6214) should return 1553.4.
assert.equal(launchFuel(6214), 1553.4);
function launchFuel(payload) {
return payload;
}
function launchFuel(payload) {
let totalMass = payload;
let additionalFuel = totalMass / 5;
let totalFuel = additionalFuel;
while (additionalFuel >= 1) {
totalMass += additionalFuel;
additionalFuel = (totalMass / 5) - totalFuel;
totalFuel += additionalFuel;
}
return Number(totalFuel.toFixed(1));
}