Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I’m failing tests 9 and 10.

Why is test 9 considered “Insufficient Funds” if there is more than the 50 cents needed in the drawer?

Similar to above, why is test 10 failing if there is the exact amount in the drawer?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang='en'>
  <head>
    <meta charset='utf-8' />
    <meta content='viewport' width='width=device-width, initial-scale=1.0' />
    <link rel='stylesheet' href='./styles.css' />
    <title>Cash Register</title>
  </head>
  <body>
    <div id='block'>
      <input id='cash' />
      <div id='change-due'></div>
      <button id='purchase-btn'>Purchase</button>
    </div>
    <script src='./script.js'></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
/*
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]
];
*/
let price = 19.5;
let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const purchaseButton = document.getElementById('purchase-btn');
const changeDueDiv = document.getElementById('change-due');
const cashInput = document.getElementById('cash');

const purchase = () => {
  const userInput = cashInput.value;
  
  const baseValues = {
    "PENNY": .01,
    "NICKEL": .05,
    "DIME": .10,
    "QUARTER": .25,
    "ONE": 1.00,
    "FIVE": 5.00,
    "TEN": 10.00,
    "TWENTY": 20.00,
    "ONE HUNDRED": 100.00
  }

  let totalCID = 0;
  for (let i of cid) {
    totalCID += i[1];
  }

  totalCID = totalCID.toFixed(2);

  let changeOwed = userInput - price;

  const changeArray = [];

  if (userInput < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  } else if (userInput == price) {
    changeDueDiv.innerText = "No change due - customer paid with exact cash";
    return;
  } else if (changeOwed > totalCID) {
    changeDueDiv.innerText = "Status: INSUFFICIENT_FUNDS";
    return;
  } else {
    cid = cid.reverse();

    for (let j of cid) {
      let tempCashValue = [j[0], 0];

      while (changeOwed >= baseValues[j[0]] && j[1] > 0) {
        tempCashValue[1] += baseValues[j[0]];
        j[1] -= baseValues[j[0]];
        changeOwed -= baseValues[j[0]];
        changeOwed = changeOwed.toFixed(2);
      }

      if (tempCashValue[1] > 0) {
        changeArray.push(tempCashValue);
      }
    }
  }
  const retArrOpen = [];
  for (let i = 0; i < changeArray.length; i++) {
    retArrOpen.push(`\n${changeArray[i][0]}: $${changeArray[i][1].toFixed(2)}`);
  }

  const retArrCid = [];
  for (let i = 0; i < cid.length; i++) {
    retArrCid.push(`\n${cid[i][0]}: $${cid[i][1].toFixed(2)}`);

  }
  
  if (changeOwed > 0) {
    changeDueDiv.innerText = "Status: CLOSED\nChange: "+retArrCid;
    return;
  }

  changeDueDiv.innerText = "Status: OPEN\nChange: "+retArrOpen;
  return;
}

purchaseButton.addEventListener("click", purchase);

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

because you don’t have the coins to give the change

because there is no more money in the drawer after

2 Likes