I"m on my last project and it appears to be returning correct object in every case but test results is failing one case
This test case is failing, even though my code is returning the correct object:
checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]);
My console.log(retObj) says I’m returning: {status: “INSUFFICIENT_FUNDS”, change: }
and the test result says it’s expecting {status: “INSUFFICIENT_FUNDS”, change: } and yet the test fails?
Your code so far
function checkCashRegister(price, cash, cid) {
let retObj = {};
// For summing register
const currency = [
['PENNY' , 0.01],
['NICKEL' , 0.05],
['DIME' , 0.10],
['QUARTER' , 0.25],
['ONE' , 1],
['FIVE' , 5],
['TEN' , 10],
['TWENTY' , 20],
['ONE HUNDRED' , 100]
];
// Calculate change due
let changeDue = cash - price;
// sum the cash in register and see if sufficient funds
let registerSum = cid.reduce(
(previousValue, currentValue) => currentValue[1] + previousValue, 0);
// Now figure out what needs to be returned
if (registerSum < changeDue) {
retObj['status'] = "INSUFFICIENT_FUNDS";
retObj['change'] = [];
} else if (registerSum === changeDue) {
retObj['status'] = "CLOSED";
retObj['change'] = cid;
} else {
retObj['status'] = "OPEN";
// Build change array (if possible)
retObj['change'] = [];
for (let i=cid.length-1; i>=0; i--) {
if (Math.trunc(changeDue/currency[i][1]) > 0) {
// We can give change in this denomination
// Need to see if the amount in this denomination need is greater than is what is in cid
if (Math.trunc(changeDue/currency[i][1])*currency[i][1] > cid[i][1]) {
// Amount required in this denomination is > than what is in cid...put all
retObj['change'].push([cid[i][0], cid[i][1]]);
changeDue -= cid[i][1];
changeDue = changeDue.toFixed(2);
} else {
retObj['change'].push([cid[i][0],
Math.trunc(changeDue/currency[i][1])*currency[i][1]]);
changeDue -= (Math.trunc(changeDue/currency[i][1])*currency[i][1]).toFixed(2);
changeDue = changeDue.toFixed(2);
}
}
}
// Test if unable to provide exact change and update status accordingly.
if (changeDue != 0) {
retObj['status'] = "INSUFFICIENT_FUNDS";
retObj['change'] = [];
}
}
console.log(retObj);
return retObj;
}
//checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
//checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: