Cash register problem

Hi,

When I run the test, one or more of the tests fails, but when I manually input the test values, it works. Is there something in the code that I need to change? I would greatly appreciate any help, as I’ve been working on this task for three weeks now.

// Define price and cid arrays
let price = 19.5; // Test
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]
];

// Query DOM elements
const totalPrice = document.querySelector(".total-cost > span");
const changeInDrawer = document.getElementById("change");
const costumerCash = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const changeDue = document.getElementById("change-due");

// Set the total price in the HTML
totalPrice.textContent = price;

// multiply cid elements by 100 couse of decimal errors and reverse
let reversedCid = cid.map((bill) => [bill[0], Math.round(bill[1] * 100)]).reverse()
console.log(reversedCid)

//eventlisten for purchase button
purchaseBtn.addEventListener('click', () => {
    console.log(costumerCash.value, price);
    if (price * 100 > costumerCash.value * 100) {
        alert("Customer does not have enough money to purchase the item");
    } else if (price * 100 === costumerCash.value * 100) {
        changeDue.textContent = "No change due - customer paid with exact cash";
    } else {
        drawer();
    };
});

// make function for the drawer

function drawer() {
    // how much should costumer get back
    let totalGetBack = parseFloat(costumerCash.value) * 100 - price * 100;

    // make an object for the bills worth multiply by 100
    const billValueObject = {
        "PENNY": 1,
        "NICKEL": 5,
        "DIME": 10,
        "QUARTER": 25,
        "ONE": 100,
        "FIVE": 500,
        "TEN": 1000,
        "TWENTY": 2000,
        "ONE HUNDRED": 10000
    }

    // total cash i drawer
    let totalCashDrawer = reversedCid.reduce((arr, curr) => arr + curr[1], 0)

    // check what type string or integer totalGetBack and totalCashDrawer is
    console.log("GetBack", totalGetBack, "total drawer", totalCashDrawer)


    // if statements
    if (totalGetBack > totalCashDrawer) {
        changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else if (reversedCid.find(bill => bill[1] === totalGetBack)) {
        let changeArray = reversedCid.map(bill => [bill[0], bill[1] / 100]);
        let changeString = changeArray.reduce((acc, curr) => acc + `${curr[0]}: $${curr[1]}\n`, "Status: CLOSED\n");
        changeDue.innerHTML = changeString.trim();
    } else {

        let changeArray = [];

        // start to loop through the drawer
        for (let i = 0; i < reversedCid.length; i++) {
            //find the type of bill
            let billName = reversedCid[i][0];
            //find the value of that bill 
            let billValue = billValueObject[billName];
            let amountInDrawer = reversedCid[i][1];
            let amountToReturn = 0;
            // check if billValue is less than totalGetBack

            // Check bills/coins  available and billValue is less than totalGetBack
            while (totalGetBack >= billValue && amountInDrawer > 0) {
                totalGetBack -= billValue;
                amountInDrawer -= billValue;
                amountToReturn += billValue
            }

            if (amountToReturn > 0) {
                changeArray.push([billName, amountToReturn / 100]);
            }

        }

        // check if we could provide the exact change
        if (totalGetBack > 0) {
            changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
        } else {
            changeDue.innerHTML = changeArray.reduce((arr, curr) => arr + `${curr[0]}: $${curr[1]}\n`, `Status: OPEN\n`)
        }

    }
}

Kind regards
Dan

Refer to this post and comment, it looks like a very similar problem

1 Like

This particular global variable is spoiling the tests for you. In general, you should avoid global variables where you can. Here, this global variable means that Cid can never be changed after this point in your code, but the tests need to do exactly that.

I could kiss you, thats awesome it worked

1 Like

Thousand thanks. It worked !

1 Like