Build a Cash Register Project - Build a Cash Register

This test case is failing when running tests, although it passes manually.
When price is 19.5 , the value in the #cash element is 20 , cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]] , and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: CLOSED PENNY: $0.5" .

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]];
let values = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.1],
  ["QUARTER", 0.25],
  ["ONE", 1],
  ["FIVE", 5],
  ["TEN", 10],
  ["TWENTY", 20],
  ["ONE HUNDRED", 100]
];
let sum = 0;

const registerContainer = document.getElementById("register-container");
const changeDue = document.getElementById("change-due");
const amountDue = document.getElementById("amount-due");
const purchaseBtn = document.getElementById("purchase-btn");

const getChange = () => {
  const cash = document.getElementById("cash");

  changeDue.textContent = "";

  if (cash.value === "") {
    alert("Please enter a cash amount.");
    return;
  }

  if (price == cash.value) {
    changeDue.textContent = "No change due - customer paid with exact cash";
    return;
  }
  if (cash.value < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  
  sum = 0;
  for (let i = 0; i < cid.length; i++) {
    sum += cid[i][1];
  }
  sum = Math.round(sum * 100) / 100;

  console.log(`sum: ${sum}`);
  
  if (sum < cash.value - price) {
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    return;
  }

  let changeDueAmount = cash.value - price;
  
  let currValuesIdx = values.length - 1;
  let change = [];

  while (changeDueAmount > 0 && currValuesIdx >= 0) {
    let temp = changeDueAmount;
    let count = Math.floor(changeDueAmount / values[currValuesIdx][1]); 
    let remainder = changeDueAmount % values[currValuesIdx][1];
    if(remainder < 0.01) {
      remainder = 0;
    }

    if (count === 0) {
      currValuesIdx--;
      continue;
    }
    if (cid[currValuesIdx][1] === 0) {
      changeDueAmount = temp;
      currValuesIdx--;
      continue;
    }
    if (count * values[currValuesIdx][1] > cid[currValuesIdx][1]) {
      if (cid[currValuesIdx][1] !== 0) {
        remainder += count * values[currValuesIdx][1] - cid[currValuesIdx][1];
      }
      change.push([values[currValuesIdx][0], Math.floor(cid[currValuesIdx][1] / values[currValuesIdx][1])]);
      cid[currValuesIdx][1] = 0;
    }
    else {
      cid[currValuesIdx][1] -= count * values[currValuesIdx][1];
      change.push([values[currValuesIdx][0], count]);
    }

    currValuesIdx--;
    changeDueAmount = remainder;
    changeDueAmount = Math.round(changeDueAmount * 100) / 100;
  }
  if(changeDueAmount > 0) {
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    return;
  }

  if (sum === 0) {
    changeDue.textContent = "Status: CLOSED";
  }
  else { // register still has money
    changeDue.textContent = "Status:  OPEN"
  }

  change.forEach((el) => {
    let value = getDollarAmount(el[0], el[1])
    changeDue.textContent += ` ${el[0]}: \$${value}`
  });
};

const getDollarAmount = (denomination, count) => {
  for (let i = 0; i < values.length; i++) {
    if (values[i][0] === denomination) {
      return count * values[i][1];
    }
  }
  return 0;
}

purchaseBtn.addEventListener("click", getChange);

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Can I see the rest of your code? The HTML and CSS?

Sure, here’s the html. Don’t have any css

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="content" width="width=content-width, initial-scale=1.0">
  <link href="styles.css">
</head>
<body>
  <main>
    <form>
      <label for="cash">Enter the cash amount:</label>
      <input type="number" step="0.01" id="cash"/>
      <button type="submit" id="purchase-btn">Purchase</button>
    </form>

    <header>Amount Due:</header>
    <div id="amount-due"></div>
    <header>Change Due:</header>
    <div id="change-due"></div>
    
    <div id="register-container"></div>
  </main>
</body>
<script src="script.js"></script>
</html>

what if sum is 0.5 and cash.value - price is also 0.5?

Then we move on to see if we have denominations to provide.

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