Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I’m not sure what I’m doing wrong for the last 2 tests please help

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">
    <link rel="stylesheet" href="styles.css">
    <title>Cash Register Project</title>
</head>
<body>
    <h3>Cash Register</h3>
    <input 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; // Adjusted the price for this scenario
let cid = [
  ['PENNY', 0.5],
  ['NICKEL', 0],
  ['DIME', 0],
  ['QUARTER', 0],
  ['ONE', 0],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
];

const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const btn = document.getElementById("purchase-btn");

const calculateChange = (cash, price, cid) => {
  let change = cash - price;
  let totalCid = cid.reduce((sum, currency) => sum + currency[1], 0);

  if (change > totalCid) {
    return "Insufficient Funds";
  }

  let changeArr = [];
  let currencyUnits = {
    "ONE HUNDRED": 100,
    "TWENTY": 20,
    "TEN": 10,
    "FIVE": 5,
    "ONE": 1,
    "QUARTER": 0.25,
    "DIME": 0.10,
    "NICKEL": 0.05,
    "PENNY": 0.01
  };

  for (let i = cid.length - 1; i >= 0; i--) {
    let currencyName = cid[i][0];
    let currencyTotal = cid[i][1];
    let currencyValue = currencyUnits[currencyName];
    let currencyAmount = 0;

    while (change >= currencyValue && currencyTotal > 0) {
      change -= currencyValue;
      change = Math.round(change * 100) / 100; // To avoid floating point precision issues
      currencyTotal -= currencyValue;
      currencyAmount += currencyValue;
    }

    if (currencyAmount > 0) {
      changeArr.push([currencyName, currencyAmount]);
    }
  }

  if (change > 0) {
    return "Insufficient Funds";
  }

  if (changeArr.length === cid.length && changeArr.every((currency, index) => currency[1] === cid[index][1])) {
    return "Closed";
  }

  return changeArr;
};

const doMath = () => {
  const cash = parseFloat(cashInput.value);

  if (isNaN(cash) || 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 changeArr = calculateChange(cash, price, cid);

    if (changeArr === "Insufficient Funds") {
      changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else if (changeArr === "Closed") {
      changeDue.textContent = "Status: CLOSED " + cid.map(curr => `${curr[0]}: $${curr[1].toFixed(2)}`).join(' ');
    } else {
      changeDue.textContent = "Status: OPEN - " + changeArr.map(curr => `${curr[0]}: $${curr[1].toFixed(2)}`).join(' ');
    }
  }
};

btn.addEventListener("click", doMath);

/* file: styles.css */
body {
    font-family: Arial, sans-serif;
    background-color: #EAEBD0; 
color: #4A5D23;
display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
}

input {
    padding: 10px;
    margin: 10px 0;
    border: 2px solid #A1B57D; 
    
    border-radius: 5px;
    font-size: 16px;
}

#change-due {
    margin: 20px 0;
    padding: 10px;
    background-color: #D7E3BC;
border: 2px solid #A1B57D;
    border-radius: 5px;
    font-size: 16px;
    width: 200px;
    text-align: center;
}

button {
    padding: 10px 20px;
    margin: 10px 0;
    background-color: #A1B57D;
color: #FFFFFF; 
border: none;
    border-radius: 5px;
    font-size: 16px;
    cursor: pointer;
}

button:hover {
    background-color: #4A5D23;
}

h3 {
    color: #4A5D23;
       border-bottom: 2px solid #A1B57D; 
         padding-bottom: 5px;
    margin-bottom: 15px;
    font-size: 24px;
}

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

I have not checked, but a common mistake is to leave the denominations that are at 0, are you removing them from your output?

ok, that is not your issue. Have you tried what happens with the values from test 18? your output is Status: OPEN - PENNY: $0.50, it should be CLOSED

you do these checks, but isn’t changeArr an array?

the insufficient funds check works so i’m not sure why the closed check doesn’t

do you mean you change changeArr which you created as an array to a string? that seems really a bad idea and really prone to errors