Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:
Hello,
I’m having trouble with testing my code. While I’ve completed my code, whenever I switch out values with values from tests, my other test are marked incorrect while the current is corrected and vice versa. For instance the last 2 test are marked incorrect while tests 12 and 13 are marked correct, but when I switch around the values, the incorrect become correct and correct become incorrect. I simply touch the values and no other code. Please help!

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset="UTF-8">
    <title>Project 4</title>
    <link href='./styles.css' rel="stylesheet">
  </head>
  <body>
    <form>
      <input id='cash'></input>
      <div id='change-due'>result</div>
      <button id='purchase-btn'>buy</button>
    </form>
    <script src='./script.js'></script>
  </body>
</html>
/* file: script.js */
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]];

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

const values = [0.01, 0.05, 0.10, 0.25, 1, 5, 10, 20, 100];

let cidTotal = cid.reduce((total, current) => Math.round((total + current[1]) * 100) / 100, 0);

const displayChange = (extraCash) => {
let change = [];
for (let i = cid.length - 1; i >= 0; i--) {
  const type = cid[i][0];
  const value = values[i];
  let temp = 0;
  

  while (cid[i][1] > 0 && extraCash >= value) {
    extraCash -= value;
    cid[i][1] -= value;
    temp += value;
    extraCash = Math.round((extraCash) * 100) / 100;
    temp = Math.round((temp) * 100) / 100;
  }
  if (temp > 0) {
    change.push([type, temp]);
  }
}

if (extraCash == 0) { // exact change and some
  change.forEach((arr) => {
    result.textContent += ` ${arr[0]}: $${arr[1]}`;
  })
} else { // not exact change
  result.textContent = "Status: INSUFFICIENT_FUNDS";
  }
};

const checkCID = (extraCash) => {
  if (cidTotal < extraCash) {
    result.textContent = "Status: INSUFFICIENT_FUNDS";
  } else if (cidTotal === extraCash) {
    result.textContent = "Status: CLOSED";
    displayChange(extraCash)
  } else {
    result.textContent = "Status: OPEN";
    displayChange(extraCash);
  }
};

buyBtn.addEventListener("click", () => {
  if (cash.value < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cash.value == price) {
      result.textContent = "No change due - customer paid with exact cash"
  } else {
    checkCID(cash.value - price);
  }
});
/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

To more accurately reproduce the way tests work, the price and cid should be reassigned at the end of code. While keeping the declarations without changes at the top.

Hi could you further explain how I would reassign price and cid at the end of the code? I’m not quite sure how to do that because only an amount of cash is inputted and nothing else.

You need to move this code into one of the functions that get triggered by the purchase button.

The test cases will not run this line of code more than once when the first test runs as it is in the global scope.
So you should move it so it can run every time the cid and price are changed.

After I moved it into my checkCID, I fixed all my other issues but still fail the very last test.

Please post the newest code and I can check it later.

Actually it seems there was a bug, I just had to refresh the page and it worked. Thanks to everyone who helped out!

1 Like