Cash Register Project

I just need any advice on my code I cant seem to pass some of the test cases, 6,7,9 and 10 respectively.
If I change the const denominations to the certain out put it wants I pass al cases but the last one
Any advice will be helpful thank you

<!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>
</head>
<body>
    <input type="number" id="cash" placeholder="Enter cash amount">
    <button id="purchase-btn">Purchase</button>
    <div id="change-due"></div>

    <script src="script.js"></script>
</body>
</html>


const price = 19.5;
const cid = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100],
];

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

const formatResults = (status, change) => {
  displayChangeDue.textContent = status;
  if (change) {
    change.forEach((money) => {
      displayChangeDue.innerHTML += `<p>${money[0]}: $${money[1].toFixed(2)}</p>`;
    });
  }
};

const checkCashRegister = () => {
  const cashValue = Number(cash.value);
  const changeDue = cashValue - price;

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

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

  let totalCID = 0;
  cid.forEach((item) => {
    totalCID += item[1];
  });

  if (totalCID < changeDue) {
    displayChangeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    return;
  }

  const change = [];
  let remainingChangeDue = changeDue;

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

  for (let i = 0; i < denominations.length; i++) {
    const currencyName = denominations[i][0];
    const currencyValue = denominations[i][1];

    const index = cid.findIndex((elem) => elem[0] === currencyName);
    const cidValue = cid[index][1];

    while (remainingChangeDue >= currencyValue && cidValue > 0) {
      remainingChangeDue -= currencyValue;
      cid[index][1] -= currencyValue;
      change.push([currencyName, currencyValue]);
      remainingChangeDue = Math.round(remainingChangeDue * 100) / 100; 
    }
  }

  if (remainingChangeDue > 0) {
    displayChangeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    return;
  }

  if (changeDue === 0) {
    formatResults("Status: CLOSED", [["PENNY", cashValue]]);
    return;
  }

  formatResults("Status: OPEN", change);
};

const checkResults = () => {
  checkCashRegister();
};

purchaseBtn.addEventListener("click", checkResults);

do not have these as const, the tests work by changing these, if you make it impossible using const you are making it impossible to test your project

Thank you
I did change it and still the same issue when I run it

1 Like

whats the issue? can you elaborate a littlebit with use case.
I guess

  1. In while loop second condition should be cidValue >= currencyValue instead of cidValue > 0 .
  2. Below condition will never occur
    if (changeDue === 0) { formatResults("Status: CLOSED", [["PENNY", cashValue]]); return; }
    because you already checked
    if (cashValue === price) { displayChangeDue.textContent = "No change due - customer paid with exact cash"; return; }
    you should use ‘remainingChangeDue > 0’ if not wrong.

That I do not get past user stories 6,7,9,10 and I am honestly lost. I tried implementing what you said and still cant pass those user stories.

Thank you for the input though.

unable to see your user story 6,7,9,10.
where is test case?
what is expected result?

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