I´m trying to finish the checkCashRegister certification challenge of JS algorithms, but I´m having troubles passing the tests. It seems the object returned is not recognized at all.
However, when I run the script on the console, the returned values seems to be fine… but it´s a different story when running from the freecodecamp console…
The only tests passing are the ones of INSUFFICIENT_FUNDS.
I debugged on chrome and consoled.log all the tests that returns the correct objects, for example:
Could it be something related to units / float problems?
Could you please enlighten me?
Link to challenge:
function checkCashRegister(price, cash, cid) {
let change = cash-price;
let output = { status: null, change: [] };
let currencies = [
["PENNY", .01],
["NICKEL", .05],
["DIME", .1],
["QUARTER", .25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
function findCash(change, cid, coins = []) {
if (change === 0) {
// base case -> change = 0
output.status = "OPEN";
output.change = coins;
return (output);
} else if(checkTotal(cid) === 0 && change > 0) {
output.status = "INSUFFICIENT_FUNDS";
return (output); // no cash in CID
} else {
// recursive call
let highestUnit = currencies.reduce((highestNum, cur, index) => {
if (Number(((change % cur[1]).toFixed(2)) < change) && cid[index][1] > 0) { // if remainder is less than change & there is the specific money in CID
let partialChange = parseInt(change / cur[1]).toFixed(2) * cur[1]; // 40
highestNum = [cur[0], partialChange, index]; // overwrites previous values the last resulting the biggest one
}
return highestNum;
}, []);
// it returns an ideal value , then I calculate if there is change possible in CID
typeof highestUnit;
if(highestUnit.length === 0) {
output.status = "INSUFFICIENT_FUNDS";
return (output);
}
if (cid[highestUnit[2]][1] >= highestUnit[1]) {
remainAmt = change % highestUnit[1];
change = Number(remainAmt.toFixed(2));
coins.push([highestUnit[0], highestUnit[1]]);
cid[highestUnit[2]][1] -= highestUnit[1];
} else { // in CID there is less
// give everything there is
change -= cid[highestUnit[2]][1];
change = Number(change.toFixed(2));
coins.push([highestUnit[0], cid[highestUnit[2]][1]]);
cid[highestUnit[2]][1] = 0;
}
return findCash(change, cid, coins);
}
}