JavaScript Algorithms and Data Structures Projects - Cash Register

This code worked and I got the certification after many many hours of struggling. I’m looking for redundancies in my code. Stuff that could easily be avoided or done much better. I’m sure I got frustrated and did not go for efficient or smart code in the end.

function checkCashRegister(price, cash, cid) {
    //console.table(cid)
    if (price == cash) return null;
    let cidInPennies = cid.map(coin => [coin[0], Math.round(coin[1] * 100)]).reverse();
    //console.table(cidInPennies);
    let totalCid = 0;
    cidInPennies.forEach((curr) => {
        totalCid += curr[1]
    })
    let change = Math.round((cash - price) * 100);

    // If change is exact return CLOSED
    if (totalCid === change) return { status: "CLOSED", change: cid }

    let response = {};

    // coin values in pennies
    const coinValues = {
        "PENNY": 1,
        "NICKEL": 5,
        "DIME": 10,
        "QUARTER": 25,
        "ONE": 100,
        "FIVE": 500,
        "TEN": 1000,
        "TWENTY": 2000,
        "ONE HUNDRED": 10000
    }

    //console.log(cidInPennies)
    let newCid = [];
    let coinValue = 0;

    //calculate how much change to give per coin
    cidInPennies.forEach((coin) => {
        coinValue = coinValues[coin[0]] //denominator value in pennies eg. "HUNDRED" = 10000
        if (change >= coinValue && coin[1] > 0) {
            let changeToGive = 0;
            changeToGive = change >= coin[1] ? coin[1] : Math.floor(change / coinValue) * coinValue;

            change -= changeToGive;
            newCid.push([coin[0], changeToGive / 100])
        }
        console.log(newCid)
    })

    if (change == 0 && totalCid > 0) {
        response.status = "OPEN";
        response.change = newCid;
        console.log(response);
        return response;
    }

    else {
        response.status = "INSUFFICIENT_FUNDS";
        response.change = [];
        console.log(response);
        return response;
    }

}

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

**Challenge:**  JavaScript Algorithms and Data Structures Projects - Cash Register

**Link to the challenge:**
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

I like that solution overall.
I’ve done this one in somewhat similar fashion.
Ways to improve maybe:
move some functionality into separate functions, that will make code a bit cleaner.
for example, I you’re doing stuff like
Math.round(stuff * 100) in several places

create function like const rounding = num => Math.round(num * 100)
and call whenever need it

also, when you are doing this check:
if (price == cash)
I would use ===, if comparing two variables of same type.
I would use == if my intention would be to utilize some type coersion

1 Like

thanks for taking the time and for the tips!

I’m glad it wasn’t as bad as I thought. I was thinking that I parsed the array too many times or did a lot more math than I needed to (which is one of your points). Basically thought there’s a simple solution for this that I couldn’t think of.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.