Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I cant seem to get it past a couple of the tests, but I cant figure out why. Can I get pointed in the right direction. Thanks.

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <div id="app">
        <h1>Cash Register</h1>
        <label for="price">Price: </label>
        <input type="number" id="price" step="0.01">
        <br>
        <label for="cash">Cash: </label>
        <input type="number" id="cash" step="0.01">
        <br>
        <button id="purchase-btn">Purchase</button>
        <div id="change-due"></div>
    </div>
    <script src="script.js"></script>
</body>
</html>

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
let 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 }
];


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

    // Scenario: cash is less than price
    if (cash < price) {
        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;
    }

    // Scenario: cash is more than price
    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 change = checkCashRegister(price, cash, cid);

    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

What are the specific error messages?

Type error alertMessage undefined
Its rude to point, thanks. :slightly_smiling_face:

Thought I had it, getting same error.

The price variable needs to be accessible from outside the handler function. Otherwise, the test can’t set it to different values

Edit: Also, I guess you can’t overwrite price inside the handler because that will clear the value set by the test.


I guess the below case isn’t tested for at all.

Log out cash and do not enter anything into the input, now click the button. What do you see logged?

parseFloat("")
NaN