The test returns are all showing no pass even when console.log shows accurate return results. The first should return by default of the code and the results for the last test should return.
// Create an array of objects which hold the denominations and their values
const DENOMINATIONS = {//unit of currency.
"PENNY": 1,//index[0]
"NICKEL": 5,
"DIME": 10,
"QUARTER": 25,
"ONE": 100,
"FIVE": 500,
"TEN": 1000,
"TWENTY": 2000,
"ONE HUNDRED": 10000//index[8]
}
function checkCashregister(price, cash, cid) {
let changeSum = Math.round(cash * 100) - Math.round(price * 100);
let changeSumCheck = changeSum;
let change = [];//empty array
let status = '';//empty string
let cidSum = 0;
//will iterate from 'ONE HUNDRED' to 'PENNY'.
let filteredCid = cid.filter(item => item[1] !==0).reverse();//also will filter through denomination to verify enough change or return 'insufficient funds'.
filteredCid.forEach(item => {
let denom = item[0];//key_item(denomination).
let denomSum = item[1] * 100;//key_item value
cidSum += denomSum;
let amount = 0;
while (changeSum >= DENOMINATIONS[denom] && denomSum > 0){
//will iterate to add to change given to cust
amount += DENOMINATIONS[denom];
changeSum -= DENOMINATIONS[denom];//while decreasing denom amount from cid
denomSum -= DENOMINATIONS[denom];//while decreasing value amount from cid
}
if(amount !== 0){
change.push([denom, amount / 100]);
}
});
if (changeSum > 0){
status = 'INSUFFICIENT_FUNDS';
change = [];
}else if (changeSum == 0 && changeSumCheck == cidSum){
status = 'closed';
change = cid;
}else{
status = "open";
}
return {'status': status, 'change': change};
}
console.log(checkCashregister(19.5, 20, [
["PENNY", 0.5], ["NICKEL", 0],
["DIME", 0], ["QUARTER", 0],
["ONE", 0], ["FIVE", 0],
["TEN", 0], ["TWENTY", 0],
["ONE HUNDRED", 0]
]));