Rolls of Coins is the fourth challenge in the "28 Relevant JavaScript Interview Questions" series. In this step by step approach, I share one the ways to attack a code challenge in a context of an onsite interview.
"At the end of her shift, Amina’s tip jar is full of coins. She needs a little help in counting and stocking her hard earned loot. She wants to organize her coins in rolls so it is easy to bring back to the bank.
Write a program that will help her quickly find out how many rolls she has for each coin denomination and the remainder on each.
Your program will accept an unsorted array of coins. You can assume that each coin will be either 1, 5, 10 or 25. It should print a message like the one below:
Pennies:10 rolls - 39 left
Nickels: 25 rolls - 0 left
Dimes: 12 rolls - 49 left
Quarter: 2 rolls - 20 left".
PLEASE NOTE:
If you are following along or reading the code please know that the first line of the function getQuotientRemainder is wrong and should instead throw an error. This has been fixed in the github repo of this presentation:
function getQuotientRemainder(x, y) {
if (!y) {
// will catch falsey values: 0, false, null, '', "", ``, undefined
throw new Error('we need an x value and a y value');
}
return { quotient: parseInt(x / y), remainder: x % y };
}