Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

i can not pass the last 4 test, although i’ve tested them and they’re working

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">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HTML 5 Boilerplate</title>
    <link rel="stylesheet" href="styles.css">
  </head>
  <body>
    <h1>Cash Register Project</h1>
    <p>Enter cash from customer:</p>
    <input type="number" id="cash"/>
    <div id="change-due"></div>
    <button id="purchase-btn">Purchase</button>

	<script src="script.js"></script>
  </body>
</html>
/* file: styles.css */
body{
  display:flex;
  flex-direction:column;
  padding:1rem;
  text-align:center;
  align-items:center;
  gap:5px;
}
/* file: script.js */
let price = 3.26;
let cashInput = document.getElementById('cash')
const checkoutBtn = document.getElementById("purchase-btn")
const changeDue = document.getElementById("change-due")
const changeArr = {}

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]]

let alteredCid = [
  [cid[0][0], cid[0][1], 0.01],
  [cid[1][0], cid[1][1], 0.05],
  [cid[2][0], cid[2][1], 0.1],
  [cid[3][0], cid[3][1], 0.25],
  [cid[4][0], cid[4][1], 1],
  [cid[5][0], cid[5][1], 5],
  [cid[6][0], cid[6][1], 10],
  [cid[7][0], cid[7][1], 20],
  [cid[8][0], cid[8][1], 100, 100]
];
const insuff = () => {
  changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}

let newCid = alteredCid.reverse()
const CalcDue = (change) => {
  for (let i = 0; i < newCid.length; i++) {
    while (newCid[i][1] > 0 && change > 0 && parseInt(change / newCid[i][2])) {
      change -= newCid[i][2]
      newCid[i][1] -= newCid[i][2]
      changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2]

      if (change < 0.01 && change > 0) {

        change -= newCid[i][2]
        newCid[i][1] -= newCid[i][2]
        changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2]
      }
    }
  }
  if (change <= 0) {
    if (newCid.reduce((a, b) => a + b[1], 0) < 0.01) {
      changeDue.textContent = `Status: CLOSED ${Object.keys(changeArr).map((cur, index) => `${cur}: $${parseFloat(Object.values(changeArr)[index].toFixed(2))}`).join(' ')}`
    } else {

      changeDue.textContent = `Status: OPEN ${Object.keys(changeArr).map((cur, index) => `${cur}: $${Object.values(changeArr)[index]}`).join(' ')}`
    }
  } else {
    insuff()
  }


}


checkoutBtn.addEventListener("click", () => {
  let cash = Number(cashInput.value)
  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 change = cash - parseFloat(price)
    if (newCid.reduce((a, b) => a + b[1], 0) >= change) {

      CalcDue(change)
    }
    else {
      insuff()
    }
  }
})

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

the tests change cid, but this is not changed, it needs to be inside a function that runs when the button is clicked

okay, i did that, and all the tests passed, except for the 7th one, i think it’s for a different reason
my new script.js

let price = 3.26;
let cashInput = document.getElementById('cash')
const checkoutBtn = document.getElementById("purchase-btn")
const changeDue = document.getElementById("change-due")
const changeArr = {}

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]]

let newCid;

const insuff = () => {
  changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
}

const CalcDue = (change) => {
  for (let i = 0; i < newCid.length; i++) {
    while (newCid[i][1] > 0 && change > 0 && parseInt(change / newCid[i][2])) {
      change -= newCid[i][2]
      newCid[i][1] -= newCid[i][2]
      changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2]

      if (change < 0.01 && change > 0) {
        change -= newCid[i][2]
        newCid[i][1] -= newCid[i][2]
        changeArr[newCid[i][0]] = changeArr[newCid[i][0]] ? changeArr[newCid[i][0]] + newCid[i][2] : newCid[i][2] 
      }
    } 
  }
  if (change <= 0) {
    cid = newCid.reverse()
    if (newCid.reduce((a, b) => a + b[1], 0) < 0.01) {
      changeDue.textContent = `Status: CLOSED ${Object.keys(changeArr).map((cur, index) => `${cur}: $${parseFloat(Object.values(changeArr)[index].toFixed(2))}`).join(' ')}`
    } else {

      changeDue.textContent = `Status: OPEN ${Object.keys(changeArr).map((cur, index) => `${cur}: $${Object.values(changeArr)[index]}`).join(' ')}`
    }
  } else {
    insuff()
  }


}
checkoutBtn.addEventListener("click", () => {
  let cash = Number(cashInput.value)
  newCid = [
  [cid[0][0], cid[0][1], 0.01],
  [cid[1][0], cid[1][1], 0.05],
  [cid[2][0], cid[2][1], 0.1],
  [cid[3][0], cid[3][1], 0.25],
  [cid[4][0], cid[4][1], 1],
  [cid[5][0], cid[5][1], 5],
  [cid[6][0], cid[6][1], 10],
  [cid[7][0], cid[7][1], 20],
  [cid[8][0], cid[8][1], 100, 100]
].reverse();
  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 change = cash - parseFloat(price)
    if (newCid.reduce((a, b) => a + b[1], 0) >= change) {

      CalcDue(change)
    }
    else {
      insuff()
    }
  }
})

your changeArr, which is not an array, still have that issue of being a global variable. Try to press the Purchase button and look at the changeArr (add console.log(changeArr) as first line of checkoutBtn.addEventListener("click", () => {}

it was an array in the first place but i forgot to change the name :laughing:
i’ll try it

the problem was that i didn’t reset the changeArr value to an empty object at the end of the event listener function

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.