# Help with JavaScript Algorithms and Data Structures Projects: Cash Register

I’ve spent the last few hours watching videos and reading others’ solutions to this problem, however I can’t figure out what I’m doing wrong with this code. I’ve been able to pass all but the last test, any advice would be much appreciated!

``````const REGISTER_STATUS = {
closed: 'CLOSED',
insufficientFunds: 'INSUFFICIENT_FUNDS',
open: 'OPEN'
}

function checkCashRegister(price, cash, cid) {
let cashRegister = { status: '', change: cid};
const changeNeeded = parseFloat(cash - price).toFixed(2);
const changeAvailable = getTotalCashRegisterChange(cid);
cashRegister.status = getTotalCashRegisterStatus(changeNeeded, changeAvailable);

if (cashRegister.status === REGISTER_STATUS.insufficientFunds) {
cashRegister.change = [];

return cashRegister
}

cashRegister.change = getCustomersChange(changeNeeded, cid);

if (changeNeeded > getTotalCashRegisterChange(cashRegister.change)) {
cashRegister.status = REGISTER_STATUS.insufficientFunds;
cashRegister.change = []
}

if (cashRegister.status === REGISTER_STATUS.closed) {
cashRegister.change = [...cid]
}

return cashRegister;
}

function getCustomersChange(changeNeeded, changeInDrawer) {
const change = [];
const currencyDictionary = {
'ONE HUNDRED': 100.0,
'TWENTY': 20.0,
'TEN': 10.0,
'FIVE': 5.0,
'ONE': 1.0,
'QUARTER': 0.25,
'DIME': 0.10,
'NICKEL': 0.05,
'PENNY': 0.01
}
for (let i = changeInDrawer.length - 1; i >= 0; i--) {
const coinName = changeInDrawer[i][0];
const coinTotal = changeInDrawer[i][1];
const coinValue = currencyDictionary[coinName];
let coinAmount = (coinTotal / coinValue.toFixed(2));
let coinsToReturn = 0;

while (changeNeeded >= coinValue && coinAmount > 0) {
changeNeeded -= coinValue;
changeNeeded = changeNeeded.toFixed(2);
coinAmount--;
coinsToReturn++;
}

if (coinsToReturn > 0) {
change.push([coinName, coinsToReturn * coinValue])
}
}

return change
}

function getTotalCashRegisterStatus(changeNeeded, changeAvailable) {
if (Number(changeNeeded) > Number(changeAvailable)) {
return REGISTER_STATUS.insufficientFunds;
}

if (Number(changeAvailable) > Number(changeNeeded)) {
return REGISTER_STATUS.open;
}

return REGISTER_STATUS.closed
}

function getTotalCashRegisterChange(changeInDrawer) {
let total = 0;

for (let change of changeInDrawer) {
let changeValue = change[1];
total += change[1];
}