I have been trying to figure out what is going on with my code and I haven’t been able to. It appears to be returning the required answers, but it isn’t passing any of the tests. It is an object like the challenge prompt says and everything. Am I mission something?
function checkCashRegister(price, cash, cid) {
//Calculate the difference to return amount of change due
let diff = cash - price;
//The three output object possibilities to return later
let returns =
[
{status: "CLOSED", change: []},
{status: "INSUFFICIENT_FUNDS", change: []},
{status: "OPEN", change: []}
];
//The monetary values of each coin or bill up to $100
const monValue =
[
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
//if no change is due return object 0 from returns array
if (diff == 0) {
return returns[0];
} else {
//else loop through ever possible monetary value for $100 to one and subtract it from cash register amount and the amount of change due
for (let i = monValue.length-1; i >= 0; i--) {
//loop throught each montary amount until that amount can't be subtracted anymore or return that there is not enought change in the register
while (true) {
if (diff - monValue[i][1] >= 0){
if (cid[i][1] - monValue[i][1] < 0) {
return returns[1];
} else {
diff -= monValue[i][1];
cid[i][1] -= monValue[i][1];
returns[2].change.push(monValue[i]);
}
} else {
break;
}
}
}
//return the return object 2 in the array with the new change amounts
return returns[2];
}
}
console.log(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]]));