Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

When the price is 19.5 and cash is 20, I get:
Status: CLOSED QUARTER: $0.50.
I’m drivin myself bananas.

Your code so far

let cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100]
];

let priceInput = document.getElementById('price');
let cashInput = document.getElementById('cash');
let changeDueDiv = document.getElementById('change-due');
let purchaseBtn = document.getElementById('purchase-btn');

// Cash units and their values
const cashUnits = [
    { name: 'ONE HUNDRED', val: 100.00 },
    { name: 'TWENTY', val: 20.00 },
    { name: 'TEN', val: 10.00 },
    { name: 'FIVE', val: 5.00 },
    { name: 'ONE', val: 1.00 },
    { name: 'QUARTER', val: 0.25 },
    { name: 'DIME', val: 0.10 },
    { name: 'NICKEL', val: 0.05 },
    { name: 'PENNY', val: 0.01 }
];

function removeTrailingZero(num) {
    let nts = num.toString();
    return nts[nts.length - 1] == '0' ? nts.slice(0, -1) : nts;
}

purchaseBtn.addEventListener('click', function () {
    let price = parseFloat(priceInput.value);
    let cash = parseFloat(cashInput.value);
    let change = checkCashRegister(price, cash, cid);

    // Scenario: cash is less than price
    if (cash < price) {
        window.alert('Customer does not have enough money to purchase the item');
        return;
    }

    // Scenario: cash is equal to price
    if (cash === price) {
        changeDueDiv.textContent = 'No change due - customer paid with exact cash';
        return;
    }


    if (change.status === 'INSUFFICIENT_FUNDS') {
        changeDueDiv.textContent = 'Status: INSUFFICIENT_FUNDS';
    } else if (change.status === 'CLOSED') {
        changeDueDiv.textContent = 'Status: CLOSED PENNY: $' + register['PENNY'].toFixed(2);
    } else {
        let changeText = 'Status: OPEN ';
        for (let i = 0; i < change.change.length; i++) {
            changeText += `${change.change[i][0]}: $${change.change[i][1].toFixed(2)} `;
        }
        changeDueDiv.textContent = changeText;
    }
});

function checkCashRegister(price, cash, cid) {
    let output = { status: null, change: [] };
    let change = cash - price;

    // Transform CID array into drawer object
    let register = cid.reduce(function (acc, curr) {
        acc.total += curr[1];
        acc[curr[0]] = curr[1];
        return acc;
    }, { total: 0 });

    // Handle exact change
    if (register.total === change) {
        output.status = 'CLOSED';
        output.change = cid;
        return output;
    }

    // Handle insufficient funds
    if (register.total < change) {
        output.status = 'INSUFFICIENT_FUNDS';
        return output;
    }

    // Loop through the cashUnits
    let change_arr = cashUnits.reduce(function (acc, curr) {
        let value = 0;

        while (register[curr.name] > 0 && change >= curr.val) {
            change -= curr.val;
            register[curr.name] -= curr.val;
            value += curr.val;


            change = Math.round(change * 100) / 100;
        }

        if (value > 0) {
            acc.push([curr.name, value]);
        }
        return acc;
    }, []);


    if (change_arr.length < 1 || change > 0) {
        output.status = 'INSUFFICIENT_FUNDS';
        return output;
    }

    output.status = 'OPEN';
    output.change = change_arr;
    return output;
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

you should also add lower denominations even if they are 0

I dont understand what you mean.

sorry my bad, that was for a previous version that had a bug, ignore what I said

Thanks. Any help for this version would be appreciated.

Could you share your HTML as well? It’s required to fully reproduce the working state of code.

Additionally, there’s few test cases having price 19.5 and cash 20, which one is giving you incorrect result?

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