Tell us what’s happening:
My code won’t pass the tests with “OPEN” result. I’m not sure where went wrong. Can someone please help me. Thank you!!!
Your code so far
function checkCashRegister(price, cash, cid) {
// set up
let lookup = [['ONE HUNDRED', 100], ['TWENTY', 20], ['TEN', 10], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['DIME', 0.1], ['NICKEL', 0.05], ['PENNY', 0.01]];
var change = cash - price;
var originalChange = change;
const reversedCid = cid.reverse(); // reverse cid so the currency units match with lookup
let copyCid = [...reversedCid]; // only make changes to copyCid in later codes
// calculate the initial total amount of cash-in-drawer
var totalCid = 0;
for (let i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}
// different possibilities with changes
let result = [...lookup]; // for later on changing the actual changes for each currency
for (let i = 0; i < result.length; i++) {
let moneyReturned = 0;
let bill = copyCid[i][1] / result[i][1]
bill.toFixed(2);
while (moneyReturned.toFixed(2) >= result[i][1] && bill >= 1) { // when the currency is appropriate for change and there are bills for that currency
change -= result[i][1]; // update the rest of changing amount
moneyReturned += result[i][1]; // update the total amount that has been changed
bill--; // reduce the number of bills by 1
}
//as long as there are still bills for that currency, we can keep changing that same currency till it either runs out or become too big for the rest of the changing amount
// deal with the JS rounding issue here:
if (moneyReturned > 0) {
if (moneyReturned - Math.floor(moneyReturned) !== 0) {
result[i][1] = moneyReturned.toFixed(2); // toFixed method returns a string
result[i][1] = parseFloat(result[i][1]); // we need to convert the string back to a number to pass the tests
}
else {
result[i][1] = moneyReturned;
}
}
else { // when moneyReturned == 0
result[i][1] = moneyReturned; // if no money was returned for that currency, then result[i][1] would be 0
}
}
// after iterating through all the currencies for change
let sumResult = 0;
for (let j = 0; j < lookup.length; j++) {
sumResult += result[j][1];
}
sumResult = sumResult.toFixed(2);
// three outcomes:
if (totalCid == originalChange) {
return {status: "CLOSED", change: cid.reverse()};
}
else if (totalCid < originalChange || sumResult < originalChange) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}
else {
let resultFiltered = [];
for (let k = 0; k < result.length; k++) {
if (result[k][1] !== 0) {
resultFiltered.push(result[k])
}
}
return {status: "OPEN", change: resultFiltered}
}
}
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]]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register
Link to the challenge: