Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Actually my code works and when I try the test cases I got the correct results on my page. But somehow I can’t pass the tests. Can you examine my code and tell me where I am making mistakes? Thank you.

Your code so far

<!-- file: index.html -->
<!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" />
  <button id="purchase-btn">Purchase</button>
  <div id="change-due"></div>

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

</html>
/* file: styles.css */

/* file: script.js */
let price = 3.26;
let cashInDrawer = [
  ["PENNY", 1.01],
  ["NICKEL", 2.05],
  ["DIME", 3.1],
  ["QUARTER", 4.25],
  ["ONE", 90],
  ["FIVE", 55],
  ["TEN", 20],
  ["TWENTY", 60],
  ["ONE HUNDRED", 100],
];

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

let reversedCashInDrawer = [...cashInDrawer].reverse();
let denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];

btn.addEventListener("click", () => {
  output.innerHTML = "";
  let result = { status: "OPEN", change: [] };
  let cashValue = parseFloat(cash.value);
  let total = 0;
  for (let i = 0; i < cashInDrawer.length; i++) {
    total += cashInDrawer[i][1];
  }
  let changeDue = cashValue - price;
  if (changeDue < 0) {
    alert("Customer does not have enough money to purchase the item");
    return;
  } else if (cashValue === price) {
    output.innerHTML = "<p>No change due - customer paid with exact cash</p>";
    return;
  } else if (changeDue > total) {
    output.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>";
    return;
  } else if (changeDue === total) {
    result.status = "CLOSED";
  }

  for (let i = 0; i <= reversedCashInDrawer.length; i++) {
    if (changeDue > denominations[i] && changeDue > 0) {
      let count = 0;
      let total = reversedCashInDrawer[i][1];
      while (total > 0 && changeDue >= denominations[i]) {
        total -= denominations[i];
        changeDue = parseFloat((changeDue -= denominations[i]).toFixed(2));
        count++;
      }
      if (count > 0) {
        result.change.push([
          reversedCashInDrawer[i][0],
          count * denominations[i],
        ]);
      }
    }
  }

  if (changeDue > 0) {
    return (output.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>");
  }

  output.innerHTML = `<p>Status: ${result.status}</p>`;
  result.change.map(
    (money) => (output.innerHTML += `<p>${money[0]}: $${money[1]}</p>`)
  );

  return;
});

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15

Challenge Information:

Build a Cash Register Project - Build a Cash Register

hi, and welcome to the forum.

I noticed that you renamed the variable cid to cashInDrawer at some point.
This may be one of the issues you are having as there is an automated test script that is expected to find this variable and is expecting your code to use it (cid not cashInDrawer)

1 Like