Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I have fully created the Build a Cash Register Project. However, only one test is passing. When I input the values manually I get the expected results letter for letter but it won’t pass me for some reason.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="./styles.css">
  </head>
  <body>
    <input id="cash" type="number">
    <div id="change-due"></div>
    <button id="purchase-btn">Convert</button>
  </body>
  <script src="./script.js"></script>
</html>
/* file: styles.css */

/* file: script.js */
const cashInput = document.getElementById("cash");
const changeText = document.getElementById("change-due");
const button = document.getElementById("purchase-btn");

let price = 19.5;
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]
];

const conversion = {
  "PENNY": 0.01,
  "NICKEL": 0.05,
  "DIME": 0.1,
  "QUARTER": 0.25,
  "ONE": 1,
  "FIVE": 5,
  "TEN": 10,
  "TWENTY": 20,
  "ONE HUNDRED": 100,
}

const totalCash = Number(cid.reduce((acc, cur) => acc + cur[1], 0).toFixed(2));

const moneyChecker = (changeDue, statusCheck) => {
  let results = {
  status: statusCheck,
  change: []
  }

  for (let i = cid.length - 1; i >= 0; i--) {
    if (changeDue >= conversion[cid[i][0]] && changeDue > 0) {
      const timesInto = Math.floor(changeDue / conversion[cid[i][0]]);
      if (conversion[cid[i][0]] * timesInto > cid[i][1]) {
        const timesInto = Math.floor(cid[i][1] / conversion[cid[i][0]])
        changeDue -= timesInto * conversion[cid[i][0]];
        changeDue = changeDue.toFixed(2);
        if (timesInto > 1) {
          results.change.push(`${cid[i][0]}: $${conversion[cid[i][0]] * timesInto}`)
        }
      } else {
        changeDue -= timesInto * conversion[cid[i][0]];
        changeDue = changeDue.toFixed(2);
        if (timesInto > 1) {
          results.change.push(`${cid[i][0]}: $${conversion[cid[i][0]] * timesInto}`)
        }
      }
    }
  }
  changeText.textContent = `Status: ${results.status} ${results.change.join(" ")}`
}

button.addEventListener("click", () => {
  const cash = Number(cashInput.value);
  let changeDue = (cash - price).toFixed(2);
  console.log(totalCash)
  console.log(changeDue)
  
  if (totalCash < changeDue) {
    changeText.textContent = "Status: INSUFFICIENT_FUNDS";
  } else if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cash === price) {
    changeText.textContent = "No change due - customer paid with exact cash";
  } else if (totalCash == changeDue) {
    moneyChecker(changeDue, "CLOSED");
  } else {
    moneyChecker(changeDue, "OPEN");
  }
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

you can’t have calculations in the global scope, the tests change cid and price

1 Like

Global variables like this aren’t rerun when your function is called multiple times

Thanks for letting me know, somehow I still have two tests not passing but I’ll look into them tomorrow!