Build a Cash Register Project, not passing test #17

So my code passes all the tests except #17, even though it should, because it works as specified in this test :frowning:

Failed:17. When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, but the individual denomination amounts make it impossible to return needed change, when the #purchase-btn element is clicked, the value in the #change-due element should be "Status: INSUFFICIENT_FUNDS"

Here’s my code:

HTML

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cash Register</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <main>
        <div class="first">
            <div class="header">
        <h1>Cash Register</h1>
            </div>
            <p id="total"> Total: <span id="total-span">$1.87</span></p>
        <label for="cash">Enter customer's cash: </label><input type="number" name="cash" id="cash">
        <button id="purchase-btn">Purchase</button>
        <div id="cid-texts">
            <p class="cid-text">Pennies: <span class="cid-text-span"></span></p>
            <p class="cid-text">Nickels: <span class="cid-text-span"></span></p>
            <p class="cid-text">Dimes: <span class="cid-text-span"></span></p>
            <p class="cid-text">Quarters: <span class="cid-text-span"></span></p>
            <p class="cid-text">Ones: <span class="cid-text-span"></span></p>
            <p class="cid-text">Fives: <span class="cid-text-span"></span></p>
            <p class="cid-text">Tens: <span class="cid-text-span"></span></p>
            <p class="cid-text">Twenties: <span class="cid-text-span"></span></p>
            <p class="cid-text">Hundreds: <span class="cid-text-span"></span></p>
        </div>
        </div>
        <div id="change-due">
            <p id="change-due-text"></p>
        </div>
    </main>
    <script src="script.js"></script>
</body>
</html>

And here is JavaScript:

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]
];
const total = document.getElementById("total-span");
const cashInput = document.getElementById("cash");
const purchaseBtn = document.getElementById("purchase-btn");
const cidTexts = document.querySelectorAll(".cid-text-span");
const changeDueText = document.getElementById("change-due-text");
const denominations = [100, 20, 10, 5, 1, 0.25, 0.10, 0.05, 0.01];

const updateCidTexts = () => {
  for (let i = 0; i < cidTexts.length; i++) {
    cidTexts[i].innerText = `$${cid[i][1]}`;
  };
  total.textContent = `$${price}`;
};


const purchase = () => {

  const customerCash = parseFloat(cashInput.value) ;

  if(customerCash < price){
    alert("Customer does not have enough money to purchase the item");
    return;
  }
  if(customerCash === price){
    changeDueText.textContent = "No change due - customer paid with exact cash";
    return;
  }

  changeDueText.innerHTML = ``;

  let change = parseFloat((customerCash - price).toFixed(2));
  let fullCidSum = 0;
  let changeCidSum = 0;
  // Full cash-in-drawer sum
  cid.forEach(cidEl => {
    fullCidSum += cidEl[1];
    fullCidSum = parseFloat(fullCidSum.toFixed(2));
  });

  // Cash-in-drawer sum of money that can be paid for change
  for(let i = 0; i < cid.length; i++){
    if(denominations[i] <= change && cid[cid.length - i - 1][1] >= denominations[i]){
      changeCidSum += cid[cid.length - i - 1][1];
      changeCidSum = parseFloat(changeCidSum.toFixed(2))
    }
  };
  
  if(changeCidSum < change){
    changeDueText.innerHTML = `Status: INSUFFICIENT_FUNDS`;
    return;
  } else if (fullCidSum === change){
    changeDueText.innerHTML = `Status: CLOSED <br>`;
    giveChange(change);
    updateCidTexts();
    return;
  } else {
    changeDueText.innerHTML = `Status: OPEN <br>`;
    giveChange(change);
    updateCidTexts();
    return;
  }

}

const giveChange = (change) => {

  let result = []

  while (change > 0){
    denominations.forEach((deno, index) => {
      while(deno <= change && cid[cid.length - index - 1][1] >= deno){
        change -= deno;
        change = parseFloat(change.toFixed(2));
        cid[cid.length - index - 1][1] -= deno;
        cid[cid.length - index - 1][1] = parseFloat((cid[cid.length - index - 1][1]).toFixed(2));
        result[index] ? result[index] += deno : result[index] = deno;
        result[index] = parseFloat((result[index]).toFixed(2));
      };
      if(result[index]){
        changeDueText.innerHTML += `${cid[cid.length - index - 1][0]}: $${result[index]} <br>`;
      }
    });
  };

}

updateCidTexts();
purchaseBtn.addEventListener("click", purchase);
cashInput.addEventListener("keydown", (e) => {
  if (e.key === "Enter"){
    purchase();
  }
}); 




Here’s the result when the price and cid are:

let price = 20;
let cid = [
  ["PENNY", 0], 
  ["NICKEL", 0], 
  ["DIME", 0], 
  ["QUARTER", 0], 
  ["ONE", 18], 
  ["FIVE", 0], 
  ["TEN", 0], 
  ["TWENTY", 0], 
  ["ONE HUNDRED", 100]
];


The cid sum is greater than the change due, but it can’t be returned because the denominations don’t allow it — everything in the task is fulfilled, but it still fails :frowning:

try running multiple times because I heard there is an issue with this testcase and it may pass if you re-run a few times.

Did it multiple times, but it keeps failing this one test. Also while running the tests, browser often shows that the website is not responding :confused:

ah okay, you have an infinite loop issue:
If you open the console you can see this message:


Potential infinite loop detected on line 81. Tests may fail if this is not changed.

You can recreate this message by changing the cid to:

let cid = [
  ['PENNY', 0],
  ['NICKEL', 0],
  ['DIME', 3.1],
  ['QUARTER', 0],
  ['ONE', 0],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
];

and then typing 2 into the cash input and click purchase

1 Like

Thanks, solved the loop and now I’ve passed :slight_smile:

1 Like

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