Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

The last TC seems to be failing even though I get the exact same output.
image

I even tried diffchecker to find a difference, but its the same.

### Your code so far

// script.js

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 trigger = () => {
      const cash = parseFloat(document.getElementById("cash").value);
      const out = document.getElementById("change-due");
    if(cash<price){
          alert("Customer does not have enough money to purchase the item");
    } else if(cash===price){
          out.innerHTML = "No change due - customer paid with exact cash";
    } else {
        const result = checkCashRegister(price, cash, cid);
        out.innerHTML = "Status: "+result.status
        console.log(result.change);
        for(const [K, V] of result.change){
          out.innerHTML += `<br/>${K}: \$${V}`;
        }
    }
}

function checkCashRegister(price, cash, cid) {
  let change = cash - price;
  let changeArr = [];
  var currency = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100],
  ];
  currency = currency.reverse();
  cid = cid.reverse();

  for (let i = 0; i < currency.length; i++) {
    if (change === 0) changeArr.push([currency[i][0], 0]);
    else if (currency[i][1] <= change) {
      if (cid[i][1] > change) {
        var denomination = Math.floor(change / currency[i][1]) * currency[i][1];
        changeArr.push([currency[i][0], denomination]);
        change -= denomination;
        change = change.toFixed(2); // debug precision error
        cid[i][1] -= denomination;
        console.log(denomination.toFixed(2));
      } else if (cid[i][1] <= change) {
        changeArr.push([currency[i][0], cid[i][1]]);
        change -= cid[i][1];
        cid[i][1] = 0;
      }
    }
  }
  // console.log(changeArr);

  var cid_left = 0;
  for (let i = 0; i < cid.length; i++) {
    cid_left += cid[i][1];
  }

  return change > 0
    ? { status: "INSUFFICIENT_FUNDS", change: [] 
    } : cid_left === 0
    ? { status: "CLOSED", change: changeArr
    } : {
        status: "OPEN",
        change: changeArr.filter(x => x[1] !== 0)
      };
}

// console.log(
//   checkCashRegister(price, 20, cid)
// );

// index.html

<html>
  <head>  
    <title>Cash Register</title>
    <link type="text/css" rel="stylesheet" href="./styles.css"/>
    <link href="https://fonts.googleapis.com/css2?family=Castoro+Titling&amp;display=swap" rel="stylesheet">
    <script src="./script.js"></script>
  </head>
  <body>
    <img class="fcc_logo" src="https://cdn.freecodecamp.org/platform/universal/fcc_primary.svg" alt="freeCodeCamp Logo">
    <h1>Cash Register Project
</h1>
    <div id="change-due"></div>
    <div class="container">
      <p>Enter cash from customer:</p>
      <form class="form">
        <input id="cash" type="number" step="0.01"></input>
        <button onclick="trigger()" id="purchase-btn">Purchase</button>
      </form>
    </div>
  </body>
</html>

Not inclusing styles.css because not necessary

Output:

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

There’s bug in the last test. See:

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