Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Some tests are failing but when I manually test with the same values, it outputs exactly as the test expects. I don’t know why it is failing.
Please help.


In the above image, the left is showing that the test failed while the in the right, the manual testing shows the exact output the test expects.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <h1>Cash Register</h1>
    <input id="cash" />
    <button id="purchase-btn">Purchase</button>
    <p id="change-due"></p>
  </body>
  <script src="script.js"></script>
</html>
/* file: styles.css */

/* 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 btn = document.getElementById("purchase-btn")
const due = document.getElementById("change-due")

const units = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100],
]

btn.addEventListener('click',() => {
  let change = Number((cash.value - price).toFixed(2))
  let indrawer = Number(cid.reduce((a,b) => a+b[1],0).toFixed(2))

  if(price > Number(cash.value)) {
    alert("Customer does not have enough money to purchase the item")
    return;
  }

  if(price == Number(cash.value)) {
    due.innerText = "No change due - customer paid with exact cash"
    return;
  }

  if(change > indrawer) {
    due.innerText = "Status: INSUFFICIENT_FUNDS"
    return;
  }

  if(change == indrawer) {
    due.innerText = `Status: CLOSED ${cid.filter(x => x[1] !== 0).map(x => `${x[0]}: $${x[1]}`).join(" ")}`
    return;
  }

  cid = cid.reverse()
  let coins = units.reverse().map((u,i) => {
    let val  = 0
    while(change >= u[1] && cid[i][1] > 0) {
      val += u[1]
      cid[i][1] -= u[1]
      change -= u[1]
      change = change.toFixed(2)
    }
    return [u[0],Number(val.toFixed(2))]
  }).filter(c => c[1] > 0)

  if(change > 0) {
    due.innerText = "Status: INSUFFICIENT_FUNDS"
    return;
  }
  due.innerText = `Status: OPEN ${coins.map(x => `${x[0]}: $${x[1]}`).join(" ")}`
})

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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

when I tested your code I found that it doesn’t always work. For eg.
Given the code exactly as above but with the addition of a log statement to watch the cid value, I typed 100 in the cash input and I got the following results

The change due said:
Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04

and the cid was:
[ [ ‘ONE HUNDRED’, 100 ],
[ ‘TWENTY’, 0 ],
[ ‘TEN’, 0 ],
[ ‘FIVE’, 40 ],
[ ‘ONE’, 89 ],
[ ‘QUARTER’, 3.75 ],
[ ‘DIME’, 2.9 ],
[ ‘NICKEL’, 2.05 ],
[ ‘PENNY’, 0.97 ] ]

These two are correct so far. But then, without reloading the page, I typed
89 in the cash and clicked purchase again.
I was expecting to get the following output:
Status: OPEN ONE: $85 QUARTER: $0.5 DIME: $0.20 PENNY: $0.04
but instead I got:
Status: INSUFFICIENT_FUNDS

This suggests that the code you are using to figure out the amount of change to return is not working. I would suggest adding some logs and testing again to see where the failure is coming from.

6 posts were split to a new topic: Build a Cash Register