Tell us what’s happening:
As far I can tell my code should be working, no errors but says I should be returning something different. I’ve approached it several different ways but can’t seem to get through it. Any help at all would be appreciated!
Your code so far
//value in pennies
const BILLS = [
['ONE HUNDRED', 10000],
['TWENTY', 2000],
['TEN', 1000],
['FIVE', 500],
['ONE', 100],
['QUARTER', 25],
['DIME', 10],
['NICKEL', 5],
['PENNY', 1]
];
//returns true if change
function iHaveChange(billValue, myBillChange, requiredChange) {
return myBillChange >= billValue && requiredChange - billValue >= 1;
}
//i have no bills
function iHaveNoBills(myCash) {
let iHaveChange = false;
Object.keys(myCash).forEach(key => {
if (myCash[key] > 0) {
iHaveChange = true;
}
});
return iHaveChange;
}
//converts penny
function cidToObject(cid) {
const myCash = {};
cid.forEach(money => {
myCash[money[0]] = parseInt(money[1] * 100);
});
return myCash;
}
// return change
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 bills big to small
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 return 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: []
};
}
// 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]]
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_14_3) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.3 Safari/605.1.15
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/