Welcome young Jedi! In this Kata you must create a function that takes an amount of US currency in cents, and returns a dictionary/hash which shows the least amount of coins used to make up that amount. The only coin denominations considered in this exercise are: Pennies (1¢), Nickels (5¢), Dimes (10¢) and Quarters (25¢). Therefor the dictionary returned should contain exactly 4 key/value pairs.
Notes:
If the function is passed either 0 or a negative number, the function should return the dictionary with all values equal to 0.
If a float is passed into the function, its value should be be rounded down, and the resulting dictionary should never contain fractions of a coin.
Examples
loose_change(56) ==> {'Nickels': 1, 'Pennies': 1, 'Dimes': 0, 'Quarters': 2}
loose_change(-435) ==> {'Nickels': 0, 'Pennies': 0, 'Dimes': 0, 'Quarters': 0}
loose_change(4.935) ==> {'Nickels': 0, 'Pennies': 4, 'Dimes': 0, 'Quarters': 0}
function looseChange(cents){ const answer = { Nickels: 0, Pennies: 0, Dimes: 0, Quarters: 0 }; if(cents <= 0) { return answer; } else { cents = cents|0; } const divideFunc = { Quarters: c => { return [c % 25, c / 25|0]; }, Dimes: c => { return [c % 10, c / 10|0]; }, Nickels: c => { return [c % 5, c / 5|0]; }, Pennies: c => [0, c] }; for(let k in divideFunc) { const [remainder, result] = divideFunc[k](cents); result && (answer[k] = result); cents = remainder; if(!cents) break; } return answer; }
지난번에 했던 시간을 초, 분, 시, 일, 연으로 바꾸는거랑 로직이 거의 같다.
리팩토링 해볼 수 있을듯.