Tell us what’s happening:
im so fustrated i cant find what im doing wrong im about to just erase it all and start from scratch again can anyone please help me take a look…
Your code so far
// VALUE IS IN PENNIES TO AVOID ROUNDING ERRORS.
const BILLS = [
["ONE HUNDRED", 10000],
["TWENTY", 2000],
["TEN", 1000],
["FIVE", 500],
["ONE", 100],
["QUARTER", 25],
["DIME", 10],
["NICKEL", 5],
["PENNY", 1],
];
// RETURNS TRUE IF THEIRS CORRECT CHANGE FOR BILL IN CASH DRAW
function iHaveChange (billsValue, myBILLSchange, requiredChange) {
return myBILLSchange >= billsValue && requiredChange - billsValue >= 0;
}
function iHaveNoBILLS (myCash) {
let iHaveChange = false;
Object.keys(myCash).forEach(key => {
if (myCash[key] > 0) {
iHaveChange = true;
}
});
return iHaveChange;
}
// CONVERTS ["PENNY", 1.01] TO ["PENNY", 101]
function cidToObject (cid) {
const myCash = {};
cid.forEach(money => {
myCash[money[0]] = parseInt(money[1] * 100);
});
return myCash;
}
// RETURNS CORRECT CHANGE FOR GIVEN PURCHASE
function checkCashRegister (price, cash, cid) {
let requiredChange = cash * 100 - price * 100;
let myCash = cidToObject(cid);
let clientCash = {};
let i = 0;
// NO CHANGE? DONE.
if (requiredChange === 0) {
return {
status: "CLOSED",
change: cid
};
}
// GIVE CHANGE IN DESCENDING ORDER FROM BIG -> TO SMALL BILLS
while(i < BILLS.length && requiredChange > 0) {
let billName = BILLS[i][0];
let billValue = BILLS[i][1];
if (iHaveChange(billValue, myCash[billName], requiredChange)) {
clientCash[billName] = 0;
while(iHaveChange(billValue, myCash[billName], requiredChange)) {
clientCash[billName] += billValue / 100;
myCash[billName] = parseInt(myCash[billName] - billValue);
requiredChange -= billValue;
}
}
i++;
}
// HANDLE RETURNS OBJECT
if (requiredChange === 0) {
if (iHaveNoBILLS(myCash)) {
return {
status: "CLOSED",
change: cid
};
}
return {
status: "OPEN",
change: Object.keys(clientCash).map(key => {
let arr = [key, clientCash[key]];
return arr;
})};
}
return {
status: "INSUFFICIENT_FUNDS",
change: []
};
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36 OPR/66.0.3515.72 (Edition Campaign 34)
.
Challenge: Cash Register
Link to the challenge: