Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Hi:) could someone please tell me why my code is not passing some of the tests.
When I try the tests manually everything works, but when I try to run it, it says it doesn’t.
Please help!
(also I know my code is very messy right now and some things are redundant, but for the moment I am just concerned with passing. I hope it’s still readable)

Your code so far

<!-- file: index.html -->
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" >
    <link src="stylesheet" href="styles.css">
    <title>cash register project</title>
  </head>

  <body>
    <p id="price-display"></p>
    <input id="cash"></input>
    <p id="change-due"></p>
    <button id="purchase-btn">purchase</button>
    <p id="cid-display"></p>
    <p id="test"></p>

    <script src="script.js"></script>
  </body>
</html>
/* file: script.js */
const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceDisplay = document.getElementById("price-display");
const cidDisplay = document.getElementById("cid-display");

const test = document.getElementById("test");

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]
];  
const currency = [
  ['ONE HUNDRED', 100],
  ['TWENTY', 20],
  ['TEN', 10],
  ['FIVE', 5],
  ['ONE', 1],
  ['QUARTER', 0.25],
  ['DIME', 0.10],
  ['NICKEL', 0.05],
  ['PENNY', 0.01],
];

priceDisplay.textContent = price;
const totalCid = cid.reduce((acc, array)=> acc + array[1],0).toFixed(2);
const cidReverse = cid.reverse();

const checkCash = () => {
  const cash = parseFloat(cashInput.value);
  if(cash < price){
    alert("Customer does not have enough money to purchase the item")
    changeDue.textContent = ""
  }
  else if(cash === price){
    changeDue.textContent = "No change due - customer paid with exact cash";
  }
  else{
    checkFunds(cash)
  }
}

const checkFunds = (input) => {
  let changeDueAmount = parseFloat(input - price).toFixed(2);
  let changeDueAmount100 = changeDueAmount*100
  if(changeDueAmount100 > (totalCid*100)){
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
  }
  else if(changeDueAmount === totalCid){
    changeDue.textContent = "Status: CLOSED";
    for(let i = 0; i < cid.length; i++){
      if(parseFloat(cidReverse[i][1]) !== 0)
         { changeDue.textContent += ` ${cidReverse[i][0]}: $${cidReverse[i][1]}`}
};
  }
  else{
    returnCash(changeDueAmount100)
  }
}
const returnCash = (change) => {
  let result = [...currency];
  const originalChange = change/100;
  for(let i = 0; i < currency.length; i++){
  let returnMoney = 0;
  let bills = cidReverse[i][1]/currency[i][1];
  bills.toFixed(2);
  while(change >= (currency[i][1]*100) && bills >= 1){
    change -= (currency[i][1]*100);
    returnMoney += currency[i][1];
    bills--;
  }
  if(returnMoney > 0){
    if(returnMoney - Math.floor(returnMoney) !== 0){
      result[i][1]= returnMoney.toFixed(2)
      result[i][1] = parseFloat(result[i][1])
    }
    else{
      result[i][1] = returnMoney
    }
  }
  else{
    result[i][1] = returnMoney;
  }
  }

  let sumResult = 0;
  for(let i = 0; i < cid.length; i++){
    sumResult+=result[i][1];
  }
  sumResult = sumResult.toFixed(2);

  if(totalCid < originalChange||sumResult < originalChange){
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
  }
  else{
    changeDue.textContent = 'Status: OPEN'
    for(let i = 0; i < result.length; i++){
      if(parseFloat(result[i][1]) !== 0){
        changeDue.textContent += ` ${result[i][0]}: $${result[i][1]}`
      }
    }
  }
}

purchaseBtn.addEventListener("click", checkCash);

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Think about how variables initialized in the global space might affect the tests.

You can add the code here to the bottom of your script file to compare what the tests expect to what your code produces:

Do you understand what you write? If not try to see how you can understand it first and use other’s code here to solve yours.
Happy coding!

The main goal here is not to complete the course or the test quickly. Try all your best to see if you can solve the problem, read all the other codes here and compare them to yours, to see how you can solve it and improve. It it also great to ask for help here if you don’t understand. It doesn’t matter how much time you take to solve it, I’m sure the skill you learn here will help you later.

Tell us what’s happening:

Hi:) could someone please help me (again).
I’m not passing tests 12, 13 and 17.
at the bottom of the script file I have tested the 12. and it should pass, but it doesn’t. I’ve been working at this challenge for ages and I can’t figure out what went wrong. Please Help!
(I know the code is messy but bear with me please)

Your code so far

<!-- file: index.html -->
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" >
    <link src="stylesheet" href="styles.css">
    <title>cash register project</title>
  </head>

  <body>
    <p id="price-display"></p>
    <input id="cash"></input>
    <p id="change-due"></p>
    <button id="purchase-btn">purchase</button>
    <p id="cid-display"></p>
    <p id="test"></p>

    <script src="script.js"></script>
  </body>
</html>
/* file: script.js */
const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceDisplay = document.getElementById("price-display");
const cidDisplay = document.getElementById("cid-display");

const test = document.getElementById("test");

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]
];  
const currency = [
  ['ONE HUNDRED', 100],
  ['TWENTY', 20],
  ['TEN', 10],
  ['FIVE', 5],
  ['ONE', 1],
  ['QUARTER', 0.25],
  ['DIME', 0.10],
  ['NICKEL', 0.05],
  ['PENNY', 0.01],
];

priceDisplay.textContent = price;


const checkCash = () => {
  const cash = parseFloat(cashInput.value);
  if(cash < price){
    alert("Customer does not have enough money to purchase the item")
    changeDue.textContent = ""
  }
  else if(cash === price){
    changeDue.textContent = "No change due - customer paid with exact cash";
  }
  else{
    checkFunds(cash)
  }
}

const checkFunds = (input) => {
  const totalCid = cid.reduce((acc, array)=> acc + array[1],0).toFixed(2);
  const cidReverse = cid.reverse();
  let changeDueAmount = parseFloat(input - price).toFixed(2);
  let changeDueAmount100 = changeDueAmount*100
  if(changeDueAmount100 > (totalCid*100)){
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
  }
  else if(changeDueAmount === totalCid){
    changeDue.textContent = "Status: CLOSED";
    for(let i = 0; i < cid.length; i++){
      if(parseFloat(cidReverse[i][1]) !== 0)
         { changeDue.textContent += ` ${cidReverse[i][0]}: $${cidReverse[i][1]}`}
};
  }
  else{
    returnCash(changeDueAmount100, totalCid, cidReverse)
  }
}
const returnCash = (change, totalCid, cidReverse) => {
  let result = [...currency];
  const originalChange = change/100;
  for(let i = 0; i < currency.length; i++){
  let returnMoney = 0;
  let bills = (cidReverse[i][1]*100)/(currency[i][1]*100);
  bills.toFixed(2);
  while(change >= currency[i][1]*100 && bills >= 1){
    change -= currency[i][1]*100;
    returnMoney += currency[i][1];
    bills--;
  }
  if(returnMoney > 0){
    if(returnMoney - Math.floor(returnMoney) !== 0){
      result[i][1]= returnMoney.toFixed(2)
      result[i][1] = parseFloat(result[i][1])
    }
    else{
      result[i][1] = returnMoney
    }
  }
  else{
    result[i][1] = returnMoney;
  }
  }

  let sumResult = 0;
  for(let i = 0; i < cid.length; i++){
    sumResult+=result[i][1];
  }
  sumResult = sumResult.toFixed(2);

  if(totalCid < originalChange||sumResult < originalChange){
    changeDue.textContent = "Status: INSUFFICIENT_FUNDS"
  }
  else{
    changeDue.textContent = 'Status: OPEN'
    for(let i = 0; i < result.length; i++){
      if(parseFloat(result[i][1]) !== 0){
        changeDue.textContent += ` ${result[i][0]}: $${result[i][1]}`
      }
    }
  }
}

purchaseBtn.addEventListener("click", checkCash);

console.log("\nTest #12");
price = 3.26;
document.querySelector("#cash").value = 100;
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];
document.querySelector("#purchase-btn").click();
console.log("actual", document.querySelector("#change-due").innerText);
console.log("expect", "Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04");

/* file: styles.css */

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15

Challenge Information:

Build a Cash Register Project - Build a Cash Register

you should add the code in the file all at once, as that is how the tests test your app. Doing that you can see some potential issues for test 12:

Test #9
actual No change due - customer paid with exact cash
expected No change due - customer paid with exact cash

Test #11
actual Status: OPEN QUARTER: $0.5
expected Status: OPEN QUARTER: $0.5

Test #12
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
actual Status: INSUFFICIENT_FUNDS
expected Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04

Test #14
actual Status: INSUFFICIENT_FUNDS
expected Status: INSUFFICIENT_FUNDS

Test #16
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
Potential infinite loop detected on line 77. Tests may fail if this is not changed.
actual Status: INSUFFICIENT_FUNDS
expected Status: INSUFFICIENT_FUNDS

Test #18
actual Status: CLOSED PENNY: $0.5
expected Status: CLOSED PENNY: $0.5

you will need to fix that infinite loop

and figure out why your app behaves differently when multiple tests are executed one after the other