Cash Register Project

Hi there

Im stuck on the Cash Register Project, any advice would be great. My code passes all but test 6, 7, and 10. Even though entering the test in manually seems to be returning correct results. Its probably something silly but I can’t figure it out

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

const input = document.getElementById("cash");
const display = document.getElementById("change-due");
const btn = document.getElementById("purchase-btn");
const register = parseFloat(
  cid
    .map((a) => a[1])
    .reduce((a, b) => a + b)
    .toFixed(2)
);
let result = { status: "OPEN", change: [] };
let nums = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let units = cid.map((a) => a[1]).reverse();

const formatResults = (status, change) => {
  display.innerHTML = `<p>Status: ${status}</p>`;
  change.map(
    (m) => (display.innerHTML += `<p>${m[0]}: $${m[1]}</p>`)
  );
  return;
};

btn.addEventListener("click", () => {
  if (Number(input.value) < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }

  if (Number(input.value) === price) {
    display.innerHTML = "No change due - customer paid with exact cash";
    result.status = "CLOSED";
    return;
  }
  let change = Number(input.value) - price;
  let cid2 = cid.map((a) => a[0]).reverse(); // names
  if (change >= register) {
    result.status = "CLOSED";
  }
  if (Number(input.value) > price) {
    for (let i = 0; i < cid.length; i++) {
      if (change > 0 && change >= nums[i]) {
        let count = 0;
        while (change > 0 && change >= nums[i] && units[i]) {
          count++;
          change = parseFloat((change -= nums[i]).toFixed(2));
          units[i] = parseFloat((units[i] -= nums[i]).toFixed(2));
          console.log(change);
        }
        if (count > 0) {
          result.change.push([cid2[i], count * nums[i]]);
        }
      }
    }
  }
  if (change > 0.00) {
    return (display.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>");
  }
  formatResults(result.status, result.change);
});

Please provide the HTML as well. It’s needed to be able to fully reproduce the working state of code.

I see you are printing the change to the console. Add a couple console.log() to take a look as well at the initial values and results when tests are running, that should show some differences from what is expected.

Sorry, first time being here

<DOCTYPE html>
  <html lang='en'>

  <head>
    <meta charset='utf-8'>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel='stylesheet' href='styles.css'>
    <title>Cash Register</title>
  </head>

  <body>
    <main>
      <div id='reg'>
        <h2>Cash Register</h2>
        <input id='cash'>
        <button id='purchase-btn'>Calc</button>
        <div id='change-due'></div>
      </div>
    </main>
    <script src='script.js'></script>
  </body>

  </html>

Great! Have you added console.log() printouts to check initial values and results when running tests?

You’re going to have trouble with these variables out in the global space

Feel like i’ve tried everything, like I said, when I enter test cases manually, they look fine.

I did notice that when I enter test 6, 7 or 10 CID and price, then run tests, 8 and 9 then fail but I just cant see why

Did you try not having those variables in the global scope?

Hey, yes, hope these are better

let result = { status: "OPEN ", change: [] };
let denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let amountCid = cid.map((a) => a[1]).reverse();

Are those still in the global scope?

But these might look different during the tests, you can add some console.log(), and then check console after running tests, ie:

const formatResults = (status, change) => {
  console.log(status, change);
btn.addEventListener("click", () => {
  console.log(result)
  console.log(units)
1 Like

I see what you mean, i moved them into the function and now im only failing the final test. What is the importance there?

Those variables in the global scope don’t change when the cash register values change. Putting them inside of the function makes sure those variables use the most up to date information.

Thanks so much, moved those 3 and the register variable into my function and it has passed straight away. Frustrating to think it was that easy.

1 Like