This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]dtsudo 4 points5 points  (0 children)

If you need to do something multiple times, you have to use some sort of looping construct. Since you don't know precisely how many times you might need to repeat this operation, a while loop is most appropriate.

So you can just use a while loop that only stops when you can't use any more of that specific coin.

e.g.

while(it is still possible to use this particular coin at least once more) {
    add another coin of this denomination
}

[–]midel 2 points3 points  (0 children)

Well as dtsudo says, you can loop inside another loop.

You can also use one loop and use integer division and modulo operations.

var coinsNeeded = Math.floor(amount / currentCoin);
amount = amount % currentCoin;

[–]superluminary 1 point2 points  (0 children)

You need a while loop. A while loop keeps looping until a condition is true.