function checkCashRegister(price, cash, cid) {
let masterBook = {
PENNY: {
value: 0.01,
amount: 0
},
NICKEL: {
value: 0.05,
amount: 0
},
DIME: {
value: 0.1,
amount: 0
},
QUARTER: {
value: 0.25,
amount: 0
},
NICKEL: {
value: 0.05,
amount: 0
},
ONE: {
value: 1,
amount: 0
},
FIVE: {
value: 5,
amount: 0
},
TEN: {
value: 10,
amount: 0
},
TWENTY: {
value: 20,
amount: 0
},
ONE_HUNDRED: {
value: 100,
amount: 0
}
}
let moneyList = {
0.01:'PENNY', 0.05:'NICKEL', 0.1:'DIME', 0.25:'QUARTER',
1:'ONE', 5:'FIVE', 10:'TEN', 20:'TWENTY',100:'ONE HUNDRED'
}
let due = {
status: '',
change: []
};
let moneyArr = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
let rest = cash - price;
moneyArr = moneyArr.filter(coin => coin < rest).reverse();
let needCash = moneyArr.map(coin => moneyList[coin])
cid = cid.filter(arr => arr[1] > 0).filter(function(arr){
for(let i = 0; i < needCash.length; i++){
if (arr[0] == needCash[i]){
return true;
}
}
});
let sum = 0;
for (let i = 0; i < cid.length; i++){
sum += cid[i][1];
}
if (sum.toFixed(2) < rest) {
due.status = "INSUFFICIENT_FUNDS";
return due;
} else if (sum == rest) {
due.status = "CLOSED";
} else {
due.status = "OPEN";
}
// filter moneyArr
moneyArr = moneyArr.filter(function(coin) {
for (let i = 0; i < cid.length; i++){
if (moneyList[coin] == cid[i][0]){
masterBook[cid[i][0]].amount = cid[i][1];
return true;
}
}
})
// function
const findChange = function(a) {
for (let i = 0; i < moneyArr.length; i++){
// preliminary assignment
let coin = moneyList[moneyArr[i]];
let amount = masterBook[coin].amount;
let value = masterBook[coin].value;
// find how much money I need
let coinNeed = Math.floor(a / value);
let maxAmount = (amount / value);
let coinDue = (a % value).toFixed(2);
if (coinNeed > maxAmount) {
// not enough coin in the box
a = (a - amount).toFixed(2);
due['change'].push([coin, amount]);
} else {
a = coinDue;
if (coinNeed != 0){
due['change'].push([coin, coinNeed*value])
}
}
}
}
findChange(rest);
if (due.status == 'CLOSED') {
for (let coin in masterBook) {
if (due['change'][0][0] != coin){
due['change'].push([coin, masterBook[coin].amount]);
}
}
}
return due;
}
checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
//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]])
//should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
//checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
//should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}
Hi i made up this topic, my function return the correct answer i guess but it isn’t accepted, i don’t understand why…
here the expected answer:
checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])
should return {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}
here is my output:
{ status: ‘CLOSED’,
change: [ [ ‘PENNY’, 0.5 ], [ ‘NICKEL’, 0 ], [ ‘DIME’, 0 ],[ ‘QUARTER’, 0 ], [ ‘ONE’, 0 ],[ ‘FIVE’, 0 ],[ ‘TEN’, 0 ],[ ‘TWENTY’, 0 ], [ ‘ONE_HUNDRED’, 0 ] ] }
ok, sorry this is my first time…
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.