Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hi, i need help here, it seems to work well in the browser but fail at free code comp.

Your code so far

const purchaseBtn = document.getElementById('purchase-btn');
const cash = document.getElementById('cash');
const changeDue = document.getElementById('change-due');


const checkCashRegister = () => {
    
    let price = 1.87;
    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]
    ];


    if(cash.value < price) {
        alert('Customer does not have enough money to purchase the item');
    } else if (cash.value == price) {
        changeDue.textContent = 'No change due - customer paid with exact cash';
    } else {
        let currencyUnit = {
            "PENNY": .01,
            "NICKEL": .05,
            "DIME": .1,
            "QUARTER": .25,
            "ONE": 1,
            "FIVE": 5,
            "TEN": 10,
            "TWENTY": 20,
            "ONE HUNDRED": 100
        }
        let change = cash.value - price;
        let totalCID = Number(cid.reduce((sum, element) => sum + element[1],0).toFixed(2));
    
        if (totalCID < change) {
            return changeDue.textContent = 'Status: INSUFFICIENT_FUNDS';
        } else if( totalCID == change) {
            return changeDue.textContent = 'Status: CLOSED';        
        } else {
            let changedArr = [];
            for(let i = cid.length - 1; i >= 0; i--) {
                let currencyUnitName = cid[i][0];
                let currencyUnitValueTotal = cid[i][1];
                let currencyUnitValue = currencyUnit[currencyUnitName];
                let currencyUnitAmount = Number((currencyUnitValueTotal / currencyUnitValue).toFixed(0));
                let currencyUnitToReturn = 0;
    
                while (change >= currencyUnitValue && currencyUnitAmount > 0) {
                    change -= currencyUnitValue;
                    change = Number(change.toFixed(2));
                    currencyUnitAmount--;
                    currencyUnitToReturn++;
                }
                
    
                if(currencyUnitToReturn > 0) {
                    changedArr.push([currencyUnitName, currencyUnitToReturn * currencyUnitValue]);
                }
            }
    
            if (change > 0) {
                return changeDue.textContent = 'Status: INSUFFICIENT_FUNDS';
            } else {
                return changeDue.textContent = 'Status: OPEN';
            }
        }
    }
}
purchaseBtn.addEventListener('click', checkCashRegister);
<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

price and cid need to be global variables, so that the tests can change them

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