Javascript Cash Register

Can any one please tell if its missing or something is done wrong
Screenshot 2024-07-05 at 9.17.43 PM.

let price = 19.5;
let cid = [
    ["PENNY", 0.5],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0],
    ["ONE", 0],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
];

const cashInput = document.getElementById("cash");
const changeOutput = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");

const currencyUnits = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
];

purchaseBtn.addEventListener("click", () => {
    const cashValue = parseFloat(cashInput.value);
    const changeDue = parseFloat((cashValue - price).toFixed(2));

    if (cashValue < price) {
        alert("Customer does not have enough money to purchase the item");
        return;
    }

    if (cashValue === price) {
        changeOutput.innerText = "No change due - customer paid with exact cash";
        return;
    }

    const result = getChange(changeDue, cid);
    if (result.status === "INSUFFICIENT_FUNDS") {
        changeOutput.innerText = "Status: INSUFFICIENT_FUNDS";
    } else if (result.status === "CLOSED") {
        changeOutput.innerText = `Status: CLOSED, Change: ${formatChange(result.change)}`;
    } else {
        changeOutput.innerText = `Status: OPEN, Change: ${formatChange(result.change)}`;
    }
});

const getChange = (changeDue, cid) => {
    let totalCid = parseFloat(cid.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2));

    if (totalCid < changeDue) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    let changeArray = [];
    let remainingChange = changeDue;

    for (let i = currencyUnits.length - 1; i >= 0; i--) {
        let unitName = currencyUnits[i][0];
        let unitValue = currencyUnits[i][1];
        let amountFromUnit = 0;
        let unitInDrawer = cid[i][1];

        while (unitValue <= remainingChange && unitInDrawer >= unitValue) {
            remainingChange -= unitValue;
            unitInDrawer -= unitValue;
            amountFromUnit += unitValue;
            remainingChange = parseFloat(remainingChange.toFixed(2));
        }

        if (amountFromUnit > 0) {
            changeArray.push([unitName, amountFromUnit]);
        }
    }

    if (remainingChange > 0) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    if (totalCid === changeDue) {
        return { status: "CLOSED", change: changeArray };
    }

    return { status: "OPEN", change: changeArray };
};

const formatChange = (changeArray) => {
    return changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(", ");
};

1 - please share a link to the project

2 - please share your html

3 -

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 (').

//js
let price = 19.5;
let cid = [
    ["PENNY", 0.5],
    ["NICKEL", 0],
    ["DIME", 0],
    ["QUARTER", 0],
    ["ONE", 0],
    ["FIVE", 0],
    ["TEN", 0],
    ["TWENTY", 0],
    ["ONE HUNDRED", 0]
];

const cashInput = document.getElementById("cash");
const changeOutput = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");

const currencyUnits = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
];

purchaseBtn.addEventListener("click", () => {
    const cashValue = parseFloat(cashInput.value);
    const changeDue = parseFloat((cashValue - price).toFixed(2));

    if (cashValue < price) {
        alert("Customer does not have enough money to purchase the item");
        return;
    }

    if (cashValue === price) {
        changeOutput.innerText = "No change due - customer paid with exact cash";
        return;
    }

    const result = getChange(changeDue, cid);
    if (result.status === "INSUFFICIENT_FUNDS") {
        changeOutput.innerText = "Status: INSUFFICIENT_FUNDS";
    } else if (result.status === "CLOSED") {
        changeOutput.innerText = `Status: CLOSED, Change: ${formatChange(result.change)}`;
    } else {
        changeOutput.innerText = `Status: OPEN, Change: ${formatChange(result.change)}`;
    }
});

const getChange = (changeDue, cid) => {
    let totalCid = parseFloat(cid.reduce((sum, [_, amount]) => sum + amount, 0).toFixed(2));

    if (totalCid < changeDue) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    let changeArray = [];
    let remainingChange = changeDue;

    for (let i = currencyUnits.length - 1; i >= 0; i--) {
        let unitName = currencyUnits[i][0];
        let unitValue = currencyUnits[i][1];
        let amountFromUnit = 0;
        let unitInDrawer = cid[i][1];

        while (unitValue <= remainingChange && unitInDrawer >= unitValue) {
            remainingChange -= unitValue;
            unitInDrawer -= unitValue;
            amountFromUnit += unitValue;
            remainingChange = parseFloat(remainingChange.toFixed(2));
        }

        if (amountFromUnit > 0) {
            changeArray.push([unitName, amountFromUnit]);
        }
    }

    if (remainingChange > 0) {
        return { status: "INSUFFICIENT_FUNDS", change: [] };
    }

    if (totalCid === changeDue) {
        return { status: "CLOSED", change: changeArray };
    }

    return { status: "OPEN", change: changeArray };
};

const formatChange = (changeArray) => {
    return changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(", ");
};
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Cash Register</title>
  <script defer src="cashRegister.js"></script>
</head>
<body>
  <div>
    <label for="cash">Cash Provided: </label>
    <input type="number" id="cash" step="0.01">
  </div>
  <div id="change-due"></div>
  <button id="purchase-btn">Purchase</button>
  <script src="./script.js"></script>
</body>
</html>

*its all code *

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 (').

remove this, you don’t have this script available in the freecodecamp editor. One of the tests will pass after this.

I will continue investigating for the other

Last two are still wrong

for the other failing test, you have floating point issues. Learn more here:

tldr: do not operate with floats, only integers

Oh, it seems that the test passing was a fluke. If you fix the issue with floating point errors, the last test will also pass

my internet is not to good that this video can work so is there any other way (because the weather is not good so internet is diying)

that’s the advice given in the video.

I suggest you watch it when you can as it’s interesting, and it explains why computers can’t do decimals math.

the last two are still wrong

what have you changed?

            Anyone pleas help  I am waiting

you now know what the issue is, you should try to debug and change things on your own

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