Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

can somone explain the last test on cash register project(the only one that didn’t accepted)

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>Document</title>
  </head>
  <body>
    <input type="text " id="cash" />
    <div id="change-due"></div>
    <button id="purchase-btn">Purchase</button>
    <script src="script.js"></script>
  </body>
</html>

/* file: script.js */
let price = 19.5;
let cid = [
  ["PENNY", 0.5],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
const amount = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100],
];
let bills = [
  ["PENNY", 0],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
document.getElementById("purchase-btn").addEventListener("click", () => {
  for (let i = 0; i < bills.length; i++) {
    bills[i][1] = Math.ceil(cid[i][1] / amount[i][1]);
  }
  let cash = document.getElementById("cash").value;
  let changedue = document.getElementById("change-due");
  if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cash == price) {
    changedue.textContent = "No change due - customer paid with exact cash";
  } else {
    let text = "";
    let change = cash - price;
    let sum = 0;

    changedue.textContent = "Status: OPEN";
    for (let i = amount.length - 1; i >= 0; i--) {
      let j = 0;
      while (change >= amount[i][1] && bills[i][1] > 0) {
        j++;
        change = (change - amount[i][1]).toFixed(2);
        bills[i][1] -= 1;
        cid[i][1] = (cid[i][1] - amount[i][1]).toFixed(2);
      }
      if (j > 0) {
        text = text + ` ${amount[i][0]}: $${j * amount[i][1]}`;
      }
    }
    if (change > 0) {
      changedue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else {
      for (let i = 0; i < cid.length; i++) {
        sum = sum + cid[i][1];
      }
      console.log(cid);
      if (sum == 0) {
        changedue.textContent = `Status: CLOSED${text}`;
      } else {
        changedue.textContent = `Status: OPEN${text}`;
      }
    }
  }
});

/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

the last testcase says:

When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: CLOSED" with change due in coins and bills sorted in highest to lowest order.

An example of this is if you have this code

price = 2;
cid = [
  ["PENNY", 0.10],
  ["NICKEL", 0.40],
  ["DIME", 0],
  ["QUARTER", 1.00],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 10.0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];

and you typed 13.5 for the cash input and clicked the purchase button.

The result should be a closed status and the change has to be sorted from highest to lower (skipping any denominations that are zero).

i am already doing that that’s why the test above last one is accepted but the last one didn’t

in some case i need to display “Status: CLOSED” and sometimes “Status: CLOSED the-change

You have to try to test different things in a row like the test would. For eg put enough money to get some change with status open, immediately followed by some money to make it insufficient then put an exact amount to make the status closed and the change returned in order.

i tried that already i will send my code test it

let price = 19.5;
let cid = [
  ["PENNY", 0.5],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
const amount = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100],
];
let bills = [
  ["PENNY", 0],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0],
];
document.getElementById("purchase-btn").addEventListener("click", () => {
  for (let i = 0; i < bills.length; i++) {
    bills[i][1] = Math.ceil(cid[i][1] / amount[i][1]);
  }
  let cash = document.getElementById("cash").value;
  let changedue = document.getElementById("change-due");
  if (cash < price) {
    alert("Customer does not have enough money to purchase the item");
  } else if (cash == price) {
    changedue.textContent = "No change due - customer paid with exact cash";
  } else {
    let text = "";
    let change = cash - price;
    let sum = 0;
    changedue.textContent = "Status: OPEN";
    for (let i = amount.length - 1; i >= 0; i--) {
      let j = 0;
      while (change >= amount[i][1] && bills[i][1] > 0) {
        j++;
        change = (change - amount[i][1]).toFixed(2);
        bills[i][1] -= 1;
        cid[i][1] = (cid[i][1] - amount[i][1]).toFixed(2);
      }
      if (j > 0) {
        text = text + ` ${amount[i][0]}: $${j * amount[i][1]}`;
      }
    }
    if (change > 0) {
      changedue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else {
      for (let i = 0; i < cid.length; i++) {
        sum = sum + cid[i][1];
      }
      console.log(cid);
      if (sum == 0) {
        changedue.textContent = `Status: CLOSED${text}`;
      } else {
        changedue.textContent = `Status: OPEN${text}`;
      }
    }
  }
});

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

can you move this code outside of the global scope?
It should be setup when purchase is clicked

I tested your code and was able to make it fail by doing the following:

1- I changed the cid so it has penny set to 0.5, quarter set to 0.5 and twenty set to 20 (all else at 0)
2- the price remained 19.5
3- i typed 40.25 as the cash and clicked purchase. The correct change was shown.
Status: OPEN TWENTY: $20 QUARTER: $0.5 PENNY: $0.25
4- I pressed purchase again and left 40.25 again. This time I saw Insufficient funds message (which is expected).
Status: INSUFFICIENT_FUNDS
5- Then I deleted the 40.25 and typed instead 19.75 and pressed purchase.
The error happens here: the status did not change from Insufficient funds even though
the register has enough change to return 25 cents in pennies. The console showed this:

[ [ 'PENNY', '0.25' ],
  [ 'NICKEL', 0 ],
  [ 'DIME', 0 ],
  [ 'QUARTER', '0.00' ],
  [ 'ONE', 0 ],
  [ 'FIVE', 0 ],
  [ 'TEN', 0 ],
  [ 'TWENTY', '0.00' ],
  [ 'ONE HUNDRED', 0 ] ]

so we can see that there is enough money there.

1 Like

i just need solution for status closed that’s it and thank you

this forum is not the correct place to look for someone to write the solution code for you.
Happy to help you answer a question though (if you have one about coding).