# Cash register leaving loop early

Tell us what’s happening:
In the while loop to determine how much of each denomination is needed, \$5 is leaving the loop early, returning 10 instead of 15. Can’t see why. Can anyone point me in the right direction? Thanks.

``````
var changeInDrawer = [
{ name: 'PENNY', value: 0.01, amount: 0 },
{ name: 'NICKEL', value: 0.05, amount: 0 },
{ name: 'DIME', value: 0.1, amount: 0 },
{ name: 'QUARTER', value: 0.25, amount: 0 },
{ name: 'ONE', value: 1.0, amount: 0 },
{ name: 'FIVE', value: 5.0, amount: 0 },
{ name: 'TEN', value: 10.0, amount: 0 },
{ name: 'TWENTY', value: 20.0, amount: 0 },
{ name: 'ONE HUNDRED', value: 100.0, amount: 0 }
];

function checkCashRegister(price, cash, cid) {
var change = cash - price;
var cidTotal = 0;
var typeReturned = [];
var changeLeft = change;
var amountOfDenom;
var numberofdenom;
console.log('change: ' + change.toFixed(2));

//place change in drawer
for (var i = 0; i < cid.length; i++) {
changeInDrawer[i].amount=(cid[i][1]);
}

console.log(changeInDrawer);

for (var i = 0; i < cid.length; i++) {
cidTotal = cidTotal + cid[i][1];
}
if (cidTotal < change) {
return { status: 'Insufficient Funds', change: [] };
} else if (cidTotal == change) {
return {status: 'Closed', change: cid};
}

//calculate change in denominations
//go through each denom. is denom less than change we have left? if yes, multiply by conseq ints to highest without going over
for (var i = changeInDrawer.length - 1; i >= 0; i--) {
numberofdenom=0;
if (  changeInDrawer[i].value<= changeLeft ) {
while (numberofdenom * changeInDrawer[i].value <=changeLeft & changeInDrawer[i].amount>0) {
changeLeft = changeLeft - changeInDrawer[i].value;
changeInDrawer[i].amount=changeInDrawer[i].amount-changeInDrawer[i].value;
numberofdenom++;
}
typeReturned.push([changeInDrawer[i].name,numberofdenom * changeInDrawer[i].value]);

}

}

console.log(typeReturned);
return {status: 'OPEN',change: typeReturned};

}

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]]);

``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0`.
This is kind of silly and obvious answer, but the `while` loop conditions are no longer met, and that’s why loop moves further. Second part of the condition is still true (btw. it should be `&&` rather than `&`), so it must be something in the first condition. Nothing surprising yet, right?