Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

This is the “Cash Register” project, and I can’t do the last test. I don’t know what is the problem.

<!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</title>
</head>
<body>
    <main>
        <h1 id="title">Cash Register Project</h1>
        <div id="change-due"></div>
        <div id="input-div">
            <p>Enter cash from customer:</p>
            <input type="number" id="cash">
            <button id="purchase-btn">Purchase</button>
        </div>
        <div class="container">
            <div class="top-display">
                <p id="price">1</p>
                <div id="top-part"></div>
            </div>
            <div id="register-top">
                <div id="buttons">
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                    <div class="btn"></div>
                </div>
                <div id="drawer">
                    <p><strong>Change in drawer:</strong></p>
                </div>
            </div>
            <div id="register-bottom">
                <div id="circle"></div>
            </div>
        </div>
    </main>
    <script src="./script.js"></script>
</body>
</html>
const input = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const change = document.getElementById("change-due");
const drawText = document.getElementById("drawer");
const textPrice = document.getElementById("price");

let price = 19.5;
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
;
const coins = ["Pennies", "Nickels", "Dimes", "Quarters", "Ones", "Fives", "Tens", "Twenties", "Hundreds"]

class Drawer {
    constructor() {
        this.valNum = [];
        this.answer = [];
    }

    verified(value) {
        if (!value || isNaN(value)) {
          alert("Please provide a valid number");
          return;
        }
          this.change(Number(value).toFixed(2));
          return;
    }

    change(cash) {
      const cashInCents = Math.round(cash * 100);
      const priceInCents = Math.round(price * 100);
  
      if (cashInCents < priceInCents) {
          alert("Customer does not have enough money to purchase the item");
          return;
      }
  
      if (cashInCents === priceInCents) {
          change.textContent = "No change due - customer paid with exact cash";
          return;
      }
  
      const num = cashInCents - priceInCents;
      this.amountValue(num);
    }

    amountValue(num) {
        let respaldo = JSON.parse(JSON.stringify(cid));
        this.answer = [];
        this.valNum = [];
        let status = "";
        respaldo.forEach((coin) => {
            switch(coin[0]) {
                case "PENNY":
                  if (coin[1] != 0) {
                    this.valNum.push([parseInt(coin[1] * 100) + 1, 1]);
                  } else {
                    this.valNum.push([0, 1]);
                  }
                  break;
                  case "NICKEL":
                    console.log(coin[1])
                    if (coin[1] != 0) {
                      this.valNum.push([parseInt(coin[1] * 100 / 5) + 1, 5]);
                    } else {
                      this.valNum.push([0, 5]);
                    }
                    break;
                  case "DIME":
                    this.valNum.push([coin[1] * 100 / 10, 10]);
                    break;
                  case "QUARTER":
                    this.valNum.push([coin[1] * 100 / 25, 25]);
                    break;
                  case "ONE":
                    this.valNum.push([coin[1], 100]);
                    break;
                  case "FIVE":
                    this.valNum.push([coin[1] / 5, 500]);
                    break;
                  case "TEN":
                    this.valNum.push([coin[1] / 10, 1000]);
                    break;
                  case "TWENTY":
                    this.valNum.push([coin[1] / 20, 2000]);
                    break;
                  case "ONE HUNDRED":
                    this.valNum.push([coin[1] / 100, 10000]);
                    break;
            }
        })

        const sum = respaldo.reduce((acc, el) => acc + el[1] * 100, 0);
        if (sum < num) {
            status = "INSUFFICIENT_FUNDS";
            change.innerHTML = `<p><span class="status">Status:</span> ${status} </p><br>`;
            return;
        } else if (sum == num) {
            status = "CLOSED";
        } else {
            status = "OPEN"
        };

        this.valNum.reverse();
        respaldo.reverse();

        for (let i = this.valNum.findIndex(item => item[1] <= num); i <= 8; i++) {
            let val = 0;
            while (this.valNum[i][1] <= num && this.valNum[i][0] > 0) {
                this.valNum[i][0]--;
                val += this.valNum[i][1]
                num -= this.valNum[i][1];
                respaldo[i][1] =  (respaldo[i][1] * 100 - this.valNum[i][1])/100; 
            }
            this.answer.push([respaldo[i][0], val/100]);
            if (num === 0) {
                cid = JSON.parse(JSON.stringify(respaldo.reverse()));
                this.update(status)
                input.value = "";
                return;
            }
        };

        if (num) {
          status = "INSUFFICIENT_FUNDS";
          change.innerHTML = `<p><span class="status">Status:</span> ${status} </p><br>`;
          input.value = "";
          return;
        };
    }

    update(status) {
      cid.forEach((item, index) => {
        const numbers = [...document.querySelectorAll(".num")];
        numbers[index].textContent = `$${item[1]}`;
      })
      change.innerHTML = `<p><span class="status">Status:</span> ${status} </p><br>`;
      this.answer.forEach((item) => {
        if(item[1] != 0 || status == "CLOSED") {
          change.innerHTML += `<p><span class="coin">${item[0]}:</span> $${item[1]} </p><br>`;
        }
      })
      console.log(change.textContent);
    }
}

const drawer = new Drawer();

coins.forEach((coin, i) => {
    drawText.innerHTML += `<p>${coin}: <span class="num">$${cid[i][1]}</span></p><br>`
})

textPrice.textContent = `Total: $${price}`;

purchaseBtn.addEventListener("click", () => {
    drawer.verified(input.value);
})

input.addEventListener("change", (e) => {
    input.value = e.currentTarget.value;
})

input.addEventListener("keydown", (e) => {
  if(e.key === "Enter") {
    drawer.verified(input.value);
  }
})

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hi! This is by far too much code to expect forum helpers to navigate through without any additional context as to the error you are getting. But I think you might find some things to check by copying your code over to the free https://playcode.io/javascript.
It has far superior syntax checking compared to freecodecamp’s javascript editor.

The last test is bugged.

You will have to wait until the fix is in production (the last build was 2 weeks ago so hopefully it will happen soon).

1 Like

Oh, thanks, I was working on this for 4 hours or more, jaja. Thanks.