Created
June 27, 2021 23:49
-
-
Save theraaz/4a27fa7ec37dae03ad0f2b96bcca9412 to your computer and use it in GitHub Desktop.
calculate coins count for vending machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Problem Statement | |
* | |
* Task Description | |
A vending machine has the following denominations: 1c, 5c, 10c, 25c, 50c, and $1. | |
Your task is to write a program that will be used in a vending machine to return change. | |
Assume that the vending machine will always want to return the least number of coins or | |
notes. Devise a function getChange(M, P) where M is how much money was inserted into the | |
machine and P the price of the item selected, that returns an array of integers | |
representing the number of each denomination to return. | |
Example: | |
getChange(5, 0.99) // should return [1,0,0,0,0,4] | |
getChange(3.14, 1.99) // should return [0,1,1,0,0,1] | |
getChange(3, 0.01) // should return [4,0,2,1,1,2] | |
getChange(4, 3.14) // should return [1,0,1,1,1,0] | |
getChange(0.45, 0.34) // should return [1,0,1,0,0,0] | |
* | |
* @param {number} amount | |
* @param {number} price | |
* @return {[]} | |
*/ | |
function getChange(amount, price){ | |
amount *= 100; // convert to cents | |
price *= 100; // convert to cents | |
const availableCoins = [1, 5, 10, 25, 50, 100]; | |
const returnCoins = []; | |
const toReturn = amount - price; | |
let remaining = JSON.parse(JSON.stringify(toReturn)); | |
for (let c = availableCoins.length; c > 0; c--) { | |
const coin = availableCoins[c - 1]; | |
let _ret = 0; | |
if (remaining) { | |
_ret = coinReturn(remaining, coin); | |
remaining -= _ret * coin; | |
} | |
returnCoins[c - 1] = _ret; | |
} | |
function coinReturn(amount, coin, timesCoin = 0) { | |
if (amount >= coin) { | |
timesCoin++; | |
return coinReturn(amount - coin, coin, timesCoin); | |
} | |
return timesCoin | |
} | |
return returnCoins | |
} | |
console.log(getChange(5, 0.99)); | |
console.log(getChange(3.14, 1.99)); | |
console.log(getChange(3, 0.01)); | |
console.log(getChange(4, 3.14)); | |
console.log(getChange(0.45, 0.34)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment