function checkCashRegister(price, cash, cid) { let amount = { "ONE HUNDRED": 100, "TWENTY": 20, "TEN": 10, "FIVE": 5, "ONE": 1, "QUARTER": 0.25, "DIME": 0.1, "NICKEL": 0.05, "PENNY": 0.01 } let copy = cid.slice(); let sum = cid.map(el => (el[1])).reduce((a, b) => (a + b)); let changeDue = cash - price; let newArr = []; let result = {}; if (sum === changeDue) { result.status = 'CLOSED'; result.change = copy; } for (let i = cid.length - 1; i >= 0; i--) { let count = 0; let unit = amount[cid[i][0]]; while (unit <= changeDue && unit <= cid[i][1]) { changeDue -= unit; cid[i][1] -= unit; count++; } newArr.push([cid[i][0], count * unit]); } return result; } console.log(checkCashRegister(19.5, 20, [ ["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0] ]));