Cash register logic

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

Is the code working? If yes, please blur the code to avoid spoiling it for others.

1 Like

Yes it works. It should be blured now. Apologies.

not quite, but I fixed it (the spoiler opening tag has to go above the code)

good job with this exercise. I like that you commented your work, but I would put the comments on separate lines above the code if the comment is longer than a few words.

The while loop’s job is to subtract the value of the current currency as many times as needed till the amount owed is less than the value of the current currency or until we run out of that currency.
You could have also done this with some other type of maths (not involving repeated subtraction). For eg. what math would you do to determine how many twenties there are in a value $1,000,000? (you wouldn’t sit there and do repeated subtractions right?)

The other comment I have would be to consider using cents next time. This will save you from having to round. (unless this problem was set in a county where pennies don’t exist, and in that case, you would have to round :smile: )