Build a Cash Register Project Issue

Hello, same as my previous post, I have an issue with one of the JavaScript’s projects.
This time, its for “Build A Cash Register Project”.

I have compared my code using VSCode and freeCodeCamp’s Register App example (they both give the same results) but once again, the tests are wrong.
Here is my code:

function buyProduct() {
  const customerCash = parseFloat(document.getElementById("cash").value);
  if (customerCash < price) alert("Customer does not have enough money to purchase the item");

  else {
    const changeDisplay = document.getElementById("change-due");
    resetAnimation(changeDisplay);
    
    changeDisplay.innerHTML = returnChange(customerCash, price, cid);
    changeDisplay.style.animation = "fadeIn 0.3s ease-in-out both";
  }
}
//there are other codes there...

function sumOfCID() {
  return parseFloat(cid.reduce((acc, el) => acc + el[1], 0));
}

function returnChange(customerMoney, priceToPay, array) {
  let changeArray = [];
  let correctMoney = [['ONE HUNDRED', 100], ['TWENTY', 20], ['TEN', 10], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['DIME', 0.1], ['NICKEL', 0.05], ['PENNY', 0.01]];

  let changeCustomer = parseFloat((customerMoney - priceToPay).toFixed(2));
  let totalCID = parseFloat(array.reduce((acc, el) => acc + el[1], 0).toFixed(2));

  if (customerMoney < priceToPay) {
      window.alert("Customer does not have enough money to purchase the item");
      return;
  } else if (customerMoney === priceToPay) {
      changeArray.push("<p>No change due - customer paid with exact cash</p>");
      return changeArray.join('');
  }

  if (totalCID < changeCustomer) {
      changeArray.push("<p>Status: INSUFFICIENT_FUNDS</p>");
      return changeArray.join('');
  }
  //sans copie ça a pas marché, avec copie ça a pas marché, donc peut etre copie profonde?
  let newArray = array.map(copyArray => [...copyArray]);

  let totalChange = [];

  for (let i = 0; i < correctMoney.length; i++) {
      let currencyName = correctMoney[i][0];
      let currencyValue = correctMoney[i][1];
      let currencyAmount = 0;

      while (changeCustomer >= currencyValue && newArray[newArray.length - 1 - i][1] >= currencyValue) {
          changeCustomer = parseFloat((changeCustomer - currencyValue).toFixed(2));
          newArray[newArray.length - 1 - i][1] = parseFloat((newArray[newArray.length - 1 - i][1] - currencyValue).toFixed(2));
          currencyAmount += currencyValue;
      }

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

  if (changeCustomer > 0) {
      changeArray.push("<p>Status: INSUFFICIENT_FUNDS</p>");
      return changeArray.join('');
  }

  if (totalCID === customerMoney - priceToPay) {
      changeArray.push("<p>Status: CLOSED</p>");
  } else {
      changeArray.push("<p>Status: OPEN</p>");
  }

  for (let j = 0; j < totalChange.length; j++) {
      changeArray.push(`<p>${totalChange[j][0]}: $${totalChange[j][1].toFixed(2)}</p>`);
  }

  return changeArray.join('');
}

If you also need the HTML, CSS and eventually the entire JS code let me know, I’ll do my best to reply asap

all the code needed to reproduce your issue, that includes the HTML, yes

okay here:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div id="cashRegister">
        <h1 class="header">Cash Register Project</h1>
        <p class="description">Enter cash from customer:</p>
        <div class="userInput">
            <input type="number" id="cash" min="0"> <br>
            <button class="button buy" id="purchase-btn" type="button" onclick="buyProduct()">Purchase</button>
            <div class="changeResult" id="change-due">
                
            </div>
        </div>

        <div class="changeDisplay">
            <button class="button display" id="toggle-change" onclick="toggleChangeDisplay()">Show Change in drawer</button>
            <div id="change" class="change">
                Pennies: $<span id="val0"></span><br>
                Nickels: $<span id="val1"></span><br>
                Dimes: $<span id="val2"></span><br>
                Quarters: $<span id="val3"></span><br>
                Ones: $<span id="val4"></span><br>
                Fives: $<span id="val5"></span><br>
                Tens: $<span id="val6"></span><br>
                Twenties: $<span id="val7"></span><br>
                Hundreds: $<span id="val8"></span><br>
            </div>
        </div>
    </div>

    <div class="copyright">
        <p>Copyright © 2024 LordBugsy. All Rights Reserved.</p>
    </div>

    <script src="script.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100..900;1,100..900&display=swap');

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes rollUp {
    0% {
        opacity: 1;
    }

    100% {
        height: 0;
        opacity: 0;
    }
}

@keyframes rollDown {
    from {
        opacity: 0;
        height: 0;
    }

    to {
        height: 50%;
        opacity: 1;
    }
}

* {
    font-family: "Montserrat", sans-serif;
    font-optical-sizing: auto;
    font-style: normal;
}

body {
    background-color: #50565f;
}

#cashRegister {
    display: flex;
    text-align: center;
    flex-direction: column;
    background-color: rgb(41, 38, 38);
    width: 50%;
    margin: auto;
    padding-bottom: 30px;
    border: 2px solid transparent;
    transition: all 0.8s;
    border-radius: 15px;
    height: auto;
    box-shadow: 0 5px 2px black;
}

#cashRegister:hover {
    border: 2px solid white;
    box-shadow: 0 5px 2px rgb(161, 161, 161);
}

#cashRegister .header, #cashRegister .description, #cashRegister .change, #cashRegister #change-due {
    color: white;
}

.button {
    cursor: pointer;
    border-radius: 5px;
    color: white;
    background-color: black;
    border: none;
    font-size: 1.2rem;
    margin: 15px;
    padding: 5px 15px;
    transition: all 0.6s;
}

.button:hover {
    transform: scale(1.2);
}

.buy {
    background-color: rgb(84, 84, 186);
}

.buy:hover {
    background-color: rgb(41, 41, 180);
}

.button.display {
    background-color: rgb(16, 160, 16);
}

.button.display:hover {
    background-color: rgb(13, 112, 13);
}

.button.display.false {
    background-color: rgb(249, 64, 64);
}

.button.display.false:hover {
    background-color: red;
}

.change {
    cursor: default;
    background-color: rgb(86, 78, 68);
    max-width: 50%;
    height: 0;
    margin: auto;
    padding: 15px;
    border-radius: 15px;
    border: 2px solid transparent;
    opacity: 0;
    transition: border 0.8s;
    overflow: hidden;
}

.change:hover {
    border: 2px solid white;
}

#cash {
    border: none;
    text-align: center;
    font-size: 1.1em;
    border-radius: 5px;
}

/* #changeResult {
     opacity: 0;
    animation: fadeIn 0.3s ease-in-out 0.4s both; 
} */



.copyright {
    color: rgba(0, 0, 0, 0.63);
    text-align: center;
    font-family: "Poppins", sans-serif;
    font-weight: 300;
    font-style: normal;
}
let originalColour = true;

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


cid.forEach((element, index) => {
  const changeValue = document.getElementById(`val${index}`);
  changeValue.textContent = element[1];
});


function buyProduct() {
  const customerCash = parseFloat(document.getElementById("cash").value);
  if (customerCash < price) alert("Customer does not have enough money to purchase the item");

  else {
    const changeDisplay = document.getElementById("change-due");
    resetAnimation(changeDisplay);
    
    changeDisplay.innerHTML = returnChange(customerCash, price, cid);
    changeDisplay.style.animation = "fadeIn 0.3s ease-in-out both";
  }
}

function toggleChangeDisplay() {
  const button = document.getElementById("toggle-change");
  const changeDisplay = document.getElementById("change");
  if (originalColour) {
    button.classList.add("false");
    button.textContent = "Hide Change in Drawer";
    changeDisplay.style.animation = "rollDown 0.2s ease-in-out both"; 
    originalColour = !originalColour;
  }

  else {
    button.textContent = "Show Change in Drawer";
    button.classList.remove("false");
    originalColour = !originalColour;
    changeDisplay.style.animation = "rollUp 0.2s ease-in-out both";
  }
}

function resetAnimation(domElement) {
  domElement.style.animation = "none";
  domElement.offsetHeight;
  domElement.style.animation = null;
}

function sumOfCID() {
  return parseFloat(cid.reduce((acc, el) => acc + el[1], 0));
}

function returnChange(customerMoney, priceToPay, array) {
  let changeArray = [];
  let correctMoney = [['ONE HUNDRED', 100], ['TWENTY', 20], ['TEN', 10], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['DIME', 0.1], ['NICKEL', 0.05], ['PENNY', 0.01]];

  let changeCustomer = parseFloat((customerMoney - priceToPay).toFixed(2));
  let totalCID = parseFloat(array.reduce((acc, el) => acc + el[1], 0).toFixed(2));

  if (customerMoney < priceToPay) {
      window.alert("Customer does not have enough money to purchase the item");
      return;
  } else if (customerMoney === priceToPay) {
      changeArray.push("<p>No change due - customer paid with exact cash</p>");
      return changeArray.join('');
  }

  if (totalCID < changeCustomer) {
      changeArray.push("<p>Status: INSUFFICIENT_FUNDS</p>");
      return changeArray.join('');
  }

  let newArray = array.map(copyArray => [...copyArray]);

  let totalChange = [];

  for (let i = 0; i < correctMoney.length; i++) {
      let currencyName = correctMoney[i][0];
      let currencyValue = correctMoney[i][1];
      let currencyAmount = 0;

      while (changeCustomer >= currencyValue && newArray[newArray.length - 1 - i][1] >= currencyValue) {
          changeCustomer = parseFloat((changeCustomer - currencyValue).toFixed(2));
          newArray[newArray.length - 1 - i][1] = parseFloat((newArray[newArray.length - 1 - i][1] - currencyValue).toFixed(2));
          currencyAmount += currencyValue;
      }

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

  if (changeCustomer > 0) {
      changeArray.push("<p>Status: INSUFFICIENT_FUNDS</p>");
      return changeArray.join('');
  }

  if (totalCID === customerMoney - priceToPay) {
      changeArray.push("<p>Status: CLOSED</p>");
  } else {
      changeArray.push("<p>Status: OPEN</p>");
  }

  for (let j = 0; j < totalChange.length; j++) {
      changeArray.push(`<p>${totalChange[j][0]}: $${totalChange[j][1].toFixed(2)}</p>`);
  }

  return changeArray.join('');
}

The error is ReferenceError: _randomNumber is not defined. There may be an issue with the tests…

Ok, there isn’t an issue with the tests, you need to solve this error first:

Uncaught TypeError: Cannot set properties of null (setting ‘textContent’)

fixed the issue about the uncaught typeerror, but it stills displays in the console (and only half of the tests are correct) ReferenceError :_randomNumber is not defined.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div id="cashRegister">
        <h1 class="header">Cash Register Project</h1>
        <p class="description">Enter cash from customer:</p>
        <div class="userInput">
            <input type="number" id="cash" min="0"> <br>
            <button class="button buy" id="purchase-btn" type="button" onclick="buyProduct()">Purchase</button>
            <div class="changeResult" id="change-due">
                
            </div>
        </div>

        <div class="changeDisplay">
            <button class="button display" id="toggle-change" onclick="toggleChangeDisplay()">Show Change in drawer</button>
            <div id="change" class="change">
                Pennies: $<span id="val0"></span><br>
                Nickels: $<span id="val1"></span><br>
                Dimes: $<span id="val2"></span><br>
                Quarters: $<span id="val3"></span><br>
                Ones: $<span id="val4"></span><br>
                Fives: $<span id="val5"></span><br>
                Tens: $<span id="val6"></span><br>
                Twenties: $<span id="val7"></span><br>
                Hundreds: $<span id="val8"></span><br>
            </div>
        </div>
    </div>

    <div class="copyright">
        <p>Copyright © 2024 LordBugsy. All Rights Reserved.</p>
    </div>

    <script src="script.js"></script>
</body>
</html>
//...

cid.forEach((element, index) => {
  const changeValue = document.getElementById(`val${index}`);
  if (changeValue) changeValue.textContent = element[1];
});

 //...

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