Last active
September 7, 2018 00:20
-
-
Save stevenYouhana/5c160b01be02b9882c7514d56fde5bc5 to your computer and use it in GitHub Desktop.
This file contains 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
const UNITS = { | |
'ONE HUNDRED': 100, | |
'TWENTY': 20, | |
'TEN': 10, | |
'FIVE': 5, | |
'ONE': 1, | |
'QUARTER': 0.25, | |
'DIME': 0.1, | |
'NICKEL': 0.05, | |
'PENNY': 0.01 | |
}; | |
function copy(o) { | |
var output, v, key; | |
output = Array.isArray(o) ? [] : {}; | |
for (key in o) { | |
v = o[key]; | |
output[key] = (typeof v === "object" && v !== null) ? copy(v) : v; | |
} | |
return output; | |
} | |
function getChange(due, arr) { | |
let ret = {status: '', change: []}; | |
let draw = copy(arr); | |
let change = draw.reverse().map(e => { | |
let ele1 = 0; | |
let element; | |
if(due >= UNITS[e[0]] && [e[1]] > 0) { | |
while(e[1] >= UNITS[e[0]] && (due - UNITS[e[0]]) >= 0) { | |
e[1] -= UNITS[e[0]]; //update old element | |
e[1] = Math.round(e[1] * 100) / 100; | |
due -= UNITS[e[0]]; //update due ammount | |
due = Math.round(due * 100) / 100; | |
ele1 += UNITS[e[0]]; //load new element | |
ele1 = Math.round(ele1 * 100) / 100; | |
} | |
element = [e[0], ele1]; | |
return element; | |
} | |
}).filter(e => e !== undefined); | |
if(draw.filter(e => e[1] !== 0).length === 0) { | |
ret.change = []; | |
} else ret.change = change; | |
return [ret,due]; | |
} | |
function addStatus(obj,cid) { | |
if(obj[0].hasOwnProperty('change') && obj[0].hasOwnProperty('status')) { | |
if(obj[1] === 0) { | |
if(obj[0].change.length > 0 ) return {status: 'OPEN', change: obj[0].change}; | |
else return {status: 'CLOSED', change: cid}; | |
} | |
else return {status: 'INSUFFICIENT_FUNDS', change: []}; | |
} | |
else return 'Properties not found'; | |
} | |
function checkCashRegister(price, cash, cid) { | |
return addStatus(getChange((cash-price), cid), cid); | |
} | |
checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], | |
["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment