I am trying to create a javascript cash register function for the freeCodeCamp cash register project.
The description of how it should work is as follows:
Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
cid is a 2D array listing available currency.
Example cash-in-drawer array:
[["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]
The checkCashRegister() function should always return an object with a status key and a change key.
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
My code is passing all the tests at freeCodeCamp except one. 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]]}. But it is returning {change: [], status: "INSUFFICIENT_FUNDS"}.
Code:
function checkCashRegister(price, cash, cid) {
var change = {};
var changeDue = cash - price;
var cidTotal = 0;
for (let i = 0; i < cid.length; i++) {
cidTotal += cid[i][1];
}
if (changeDue == cidTotal) {
change.status = "CLOSED";
change.change = cid;
return change;
}
change.change = [];
var denominations = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
var remaining = changeDue;
var units = ["PENNY", 'NICKEL', 'DIME', 'QUARTER', 'ONE', 'FIVE', 'TEN', 'TWENTY', 'ONE HUNDRED'];
for (let i = cid.length - 1; i >= 0; i--) {
let leastCount = denominations[i];
let vid = cid[i][1]; //value in drawer for current denomination
let n = 0;
while (remaining >= leastCount && vid > 0) {
vid -= leastCount;
remaining -= leastCount;
n ++;
}
if (n > 0) {
change.change.push([units[i], n * leastCount]);
}
}
console.log(change.change); //Console.log statement 1
console.log(remaining); //Console.log statement 2
if (remaining == 0) {
change.status = "OPEN";
} else {
change.status = "INSUFFICIENT_FUNDS"
change.change = [];
}
return change;
}
console.log(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]]));
My code is passing all the tests at freeCodeCamp except one. 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]]} But it is returning {change: [], status: "INSUFFICIENT_FUNDS"}.
Adding console.log(change.change) after the for loop displays [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.03]] instead of the expected result [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]].
Adding console.log(remaining) after the for loop displays
0.009999999999994869
instead of just
0.