Cash -register-project

Hello guys, I can’t pass the test in this project and I can’t figure them out by myself. I guess I misunderstood the requirement of the quesiton so I just put part of the code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cash Register</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        input, button {
            padding: 5px;
            margin: 5px;
        }
        #change-due {
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>Cash Register</h1>
    <label for="price-input">Price:</label>
    <input type="number" id="price-input" step="0.01" placeholder="Enter price">
    <br>
    <label for="cash">Cash Provided:</label>
    <input type="number" id="cash" step="0.01" placeholder="Enter cash provided" id="cash">
    <br>
    <button id="purchase-btn">Purchase</button>
    <button id="clear-btn">Clear</button>
    <div id="change-due"></div>

    <script src="script.js"></script>
</body>
</html>
let price = 1.87;
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 currencyUnits = [
  ["PENNY", 0.01],
  ["NICKEL", 0.05],
  ["DIME", 0.10],
  ["QUARTER", 0.25],
  ["ONE", 1.00],
  ["FIVE", 5.00],
  ["TEN", 10.00],
  ["TWENTY", 20.00],
  ["ONE HUNDRED", 100.00]
];

document.getElementById('purchase-btn').addEventListener('click', checkCashRegister);
document.getElementById('clear-btn').addEventListener('click', clearResults);

function checkCashRegister() {
  price = parseFloat(document.getElementById('price-input').value);
  const cash = parseFloat(document.getElementById('cash').value);
  const resultDiv = document.getElementById('change-due');
  resultDiv.textContent = '';

  if (isNaN(price) || isNaN(cash)) {
    alert('Please provide valid inputs for price and cash provided.');
    return;
  }

  const changeDue = cash - price;
  if (changeDue < 0) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }

  if (changeDue === 0) {
    resultDiv.textContent = "No change due - customer paid with exact cash";
    return;
  }

Up to here I expect I should pass the first 8 tests, but the following two tests still fail: * When the value in the #cash element is less than price, an alert should appear with the text "Customer does not have enough money to purchase the item".

  • Waiting:When the value in the #cash element is equal to price, the value in the #change-due element should be "No change due - customer paid with exact cash". Could someone help me figure out what is going wrong? Thank you

Welcome to the forum @zhangyuxuan210915

This is the code causing the two issues.

The price variable is declared in the global scope, so you should not reassign it.

Try commenting out the code and running the tests.

Happy coding

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 (').

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