Please verify that my logic makes sense, thanks you
function checkCashRegister(price, cash, cid) {
var changeOwed = cash - price; /*returns the amount given minus the amount it costs*/
var change = []; /*Sets the change to empty array to display an amount when due change is calculated*/
var totalCid = 0; /*Initializes the total cash in drawer*/
for (var i = 0; i < cid.length; i++) {
totalCid += cid[i][1]; /*Loops through the total cash in the drawer and then adds it all together to give totalCid a new value of the actual amount in drawer */
}
if (totalCid < changeOwed) { /*if the total cash in drawer is less than change owed to person*/
return {status: "INSUFFICIENT_FUNDS", change: []}; // if the total cash in drawer is less than the change owed it returns insufficient funds to give
}
// check if cash in drawer is equal to change due
if (totalCid === changeOwed) { // if the the total cash in drawer is exactly the change of amount owed to person
return {status: "CLOSED", change: cid}; // the calculation completes and gives the cash in drawer with the amount displayed
}
// the units that are used/declared when calculating change to be given
var currencyValue = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
];
for (var i = currencyValue.length - 1; i >= 0; i--) { // loops through the contents of currencyvalue
var nameOfCurrency = currencyValue[i][0]; // Then gives the currency names a value by accessing it through bracket notation using the first array at 0 index
var valueOfCurrency = currencyValue[i][1]; // Gives the value of currency names a value of the amount it is equal to
if (changeOwed >= valueOfCurrency) { // if the change owed to persons is more than and equal to the amount of the currency
var amount = 0; // Initialize that amount to 0
while (changeOwed >= valueOfCurrency && cid[i][1] >= valueOfCurrency) { //while the change owed is more than and equal to the amount of currency and the total cash in draw is more than and equal to the amount of currency
changeOwed -= valueOfCurrency; //Subtract the value of currency from the change owed
changeOwed = Math.round(changeOwed * 100) / 100; /*Round off the value of the change owed*/
cid[i][1] -= valueOfCurrency; // Subtract the value of the cash in drawer array from the value of the currency
amount += valueOfCurrency; // add value of currency to initialized amount
}
if (amount > 0) { // If the amount to be given is more than 0
change.push([nameOfCurrency, amount]); // Push/Display the amount in the empty change array
}
}
}
if (changeOwed > 0) { // if the change owed is more than 0
return {status: "INSUFFICIENT_FUNDS", change: []}; // and the cash in drawer doesnt have that amount display or return insufficient funds
}
return {status: "OPEN", change: change}; // if the cash in drawer has that amount or more return the change owed to person
}
console.log(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]]));