Build a Cash Register

const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
let price = 3.26;
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 changeToGive = [
    ['PENNY', 0],
    ['NICKEL', 0],
    ['DIME', 0],
    ['QUARTER', 0],
    ['ONE', 0],
    ['FIVE', 0],
    ['TEN', 0],
    ['TWENTY', 0],
    ['ONE HUNDRED', 0]
];
let poep = [
    ['PENNY', 0.01],
    ['NICKEL', 0.05],
    ['DIME', 0.1],
    ['QUARTER', 0.25],
    ['ONE', 1],
    ['FIVE', 5],
    ['TEN', 10],
    ['TWENTY', 20],
    ['ONE HUNDRED', 100]
];
let sum = 0;
for (let i of cid)
    sum += i[1];
function makeTheChange( cash ) {
    for (let i = 8; i >= 0; i--) {
        while (cash >= poep[i][1] && cid[i][1] > 0) {
            cash -= poep[i][1], changeToGive[i][1] += poep[i][1], cid[i][1] -= poep[i][1];
            cash = cash.toFixed(2);
        }
    }
}

purchaseBtn.onclick = function () {
    let cash = document.getElementById("cash").value;
    console.log( cash );
    if (cash < price)
        alert("Customer does not have enough money to purchase the item");
    else if (cash == price)
        changeDue.innerText = "No change due - customer paid with exact cash";
    else if (sum < cash - price)
        changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
    else {
        if (sum == cash - price)
            changeDue.innerText = "Status: CLOSED";
        else
            changeDue.innerText = "Status: OPEN";
        makeTheChange( cash - price );
        for (let i = 8; i >= 0; i -- )
            if (changeToGive[i][1] > 0)
                changeDue.innerText += `${changeToGive[i][0]}: $${changeToGive[i][1]}`;
    }
}

why my code’s outputs are not getting accepted in testcase 12

is there any detail that i did wrong

You shouldn’t make extra global variables

1 Like

can you share your html and a link to the challenge?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="styles.css" rel="stylesheet">
    <title>Palindrome Checker</title>
</head>

<body>
    <main class="container">
        <input type="number" step="0.01" id="cash">
        <p id="change-due"></p>
        <button id="purchase-btn"></button>
    </main>
    <script src="script.js"></script>
</body>

</html>

here it is

can you share a link to the challenge please?

great, thank you! Now take a look at Jeremy’s suggestion

1 Like