curriculum/challenges/english/blocks/daily-coding-challenges-javascript/6821ebea237de8297eaee795.md
Given an integer representing the number of candles you start with, and an integer representing how many burned candles it takes to create a new one, return the number of candles you will have used after creating and burning as many as you can.
For example, if given 7 candles and it takes 2 burned candles to make a new one:
You will have burned 13 total candles in the example.
burnCandles(7, 2) should return 13
assert.equal(burnCandles(7, 2), 13);
burnCandles(10, 5) should return 12
assert.equal(burnCandles(10, 5), 12);
burnCandles(20, 3) should return 29
assert.equal(burnCandles(20, 3), 29);
burnCandles(17, 4) should return 22
assert.equal(burnCandles(17, 4), 22);
burnCandles(2345, 3) should return 3517
assert.equal(burnCandles(2345, 3), 3517);
function burnCandles(candles, leftoversNeeded) {
return candles;
}
function burnCandles(candles, leftoversNeeded) {
let totalBurned = 0;
let unusedLeftovers = 0;
while (candles > 0) {
totalBurned += candles;
const leftovers = candles + unusedLeftovers;
candles = Math.floor(leftovers / leftoversNeeded);
unusedLeftovers = leftovers % leftoversNeeded;
}
return totalBurned;
}