# Help with Cash Register Challenge! (Algorithms and Data Structures)

Tell us what’s happening:
Hi! So, I’ve been trying to find a solution for the Cash Register Challenge (in the JavaScript Algorithms and Data Structures course) but I got stuck in a little detail with my solution almost finished. What it’s happening is that my program is missing an iteration (just one) when it calculates the change cash in the second test case (the 3.26, 100 case). The total change you need is 96.74 but i’m getting 96.73! But the strange thing is that in the first test case (19.5, 20) the program gets the correct exact change (0.5). So i’m pretty confused about that. I’ve read over and over again my while loop (which is managing this money addition for the change) but I cant find why my program is doing this weird thing.

I really would appreciate if someone could help me with this, its the only thing I need to solve to finish this course!

Cash Register)

``````
function checkCashRegister(price, cash, cid) {
let currencyUnits = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100,
};

let totalCid = cid.reduce((sum, coin)=>sum + coin[1],0).toFixed(2);
let changeNeeded = cash - price;

if(changeNeeded>totalCid) {
return {status: "INSUFFICIENT_FUNDS", change: []};
}

else if(changeNeeded==totalCid) {
return {status: "CLOSED", change: [...cid]}
}

else {
//Get an array with the coins in cid which its unit value is less than the change I need to give
let cidNeeded = cid.filter(coin => currencyUnits[coin[0]]<=changeNeeded);

//Prepare the returned object
let returnValue = {
status: "OPEN",
change: [],
};

let totalSum = 0;
//Iterate on every filtered (and reversed) cid element and add a unit value until
//the changeNeeded is reached or the cash available of the coin is empty
cidNeeded.reverse().forEach(function(elementInCid){

while(totalSum + currencyUnits[elementInCid[0]] <= changeNeeded && elementInCid[1]>0) {
//console.log(currencyUnits[elementInCid[0]]); //how much is added in each iteration

//The coin and the amount used is added to returnValue.change in array format
//after checking if the coin is already in the array or not
if(returnValue.change.some(elem => elem[0]==elementInCid[0])) {
let repeatedElement = returnValue.change.find(elem => elem[0]==elementInCid[0]);
let indexOfThatElement = returnValue.change.indexOf(repeatedElement);
returnValue.change[indexOfThatElement][1] += currencyUnits[elementInCid[0]];
}
else {
returnValue.change.push([elementInCid[0],currencyUnits[elementInCid[0]]])

}

totalSum += currencyUnits[elementInCid[0]];
elementInCid[1] -= currencyUnits[elementInCid[0]];

}

});

console.log(totalSum);
//console.log(returnValue);

if(totalSum<changeNeeded){
return {status: "INSUFFICIENT_FUNDS", change: []};
}

return returnValue;

}

}

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

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]]);
``````
``````  **Your browser information:**
``````

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36`

Challenge: Cash Register