Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

For some reason, i can pass 1 to 13 with the default “cid” but not 14 to 17.
When I change to cid to only get 1 penny, I pass 14 to 17 but not anymore 11 to 13.
Anyone, can help?

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
    <h1>Cash Register Project</h1>
    <legend for="cash">Enter cash here:
    </legend>
    <input type="number" id="cash"/>
    <button id="purchase-btn">Purchase</button>
    <div id="change-due"></div>

    <div>
      <h1>Price to pay</h1>
      <p id="price-pay"></p>
    </div>
    <div>
      <h1>Cash in drawer</2>
      <div id="cash-in-drawer">
        <ul>

        </ul>
      </div>
    </div>
  <script src="./script.js"></script>
  </body>
</html>


/* file: styles.css */

/* file: script.js */
let price = 19.5;
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 ref = [['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.10],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
];

const cash = document.getElementById("cash");
const changedue = document.getElementById("change-due");
const purchasebtn = document.getElementById("purchase-btn");
document.getElementById("price-pay").innerHTML = `$${price}`;
const drawercash = document.getElementById("cash-in-drawer");

cid.forEach((val) =>
{
  drawercash.querySelector('ul').innerHTML += `<li>${val[0]}: $${val[1]}</li>`;
});



let reversecid = [...cid].reverse();

function searchValueInArray (str) {
  const row = ref.findIndex(row => row.includes(str));
  return ref[row];
}

const calculateChange = (value, cids) => {
  let change = [];
  let temp = value - price;

  cids.forEach((val) => {
    let coinType = val[0];
    let coinAmount = val[1];
    let coinValue = searchValueInArray(coinType)[1];
    
    if (temp >= coinValue && coinAmount > 0) {
      let count = 0;

     while (temp >= coinValue && coinAmount > 0) {
        temp -= coinValue;
        coinAmount -= coinValue;
        count++;
        temp = parseFloat(temp.toFixed(2));
      }

      if (count > 0) {
        change.push([coinType, (count * coinValue).toFixed(2)]);
      }
    }
  });

  if (temp > 0.01) {
    return "Status: INSUFFICIENT_FUNDS";
  }

  return change;
}


purchasebtn.addEventListener("click", () =>
{
  if(parseFloat(cash.value) < price)
  {
    alert("Customer does not have enough money to purchase the item");
  }
  else if(parseFloat(cash.value) === price)
  {
    changedue.innerHTML = "No change due - customer paid with exact cash";
  }
  else
  {
    let string = "";
    const result = calculateChange(parseFloat(cash.value), reversecid);

    if(Array.isArray(result))
    {
      string += "<p>Status: OPEN </p>";
      result.forEach((val) =>
      {
        string += `<p>${val[0]}: $${val[1]} </p>`;
      });
    }
    else
      string += result;

    changedue.innerHTML = string;
  }
});

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

If you want to manually check examples from tests, the cid and price should be changed to the values from tests at the end of your JS. That more accurately imitate what tests are actually doing.

Thanks for the reply, but I don’t want to test it because I already did it.
What I’ve said is, that when I run Tests, it passes test 1 to test 13 (included), but not test 14.
When I change “cid” according to test 14 aka

[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]
It is passe but tests from 11 to 13 don’t anymore.
What is the problem with the code? If it passes for 12 it should be for 14 as the cid is updated by the tests itself.

you need to test more
to test your code set the cid and price like this:

let cid = [];
let price = 0;

/* ALL YOUR CODE */

cid = [
  ['PENNY', 1.01],
  ['NICKEL', 2.05],
  ['DIME', 3.1],
  ['QUARTER', 4.25],
  ['ONE', 90],
  ['FIVE', 55],
  ['TEN', 20],
  ['TWENTY', 60],
  ['ONE HUNDRED', 100]
];

price = 19.5;

and find the point of failure

if the tests passing or failing changes depending on the starting value of cid, then there is something that is not working as it should.

I suspect, that if you have variables that you create in the global scope and are never changed after the button is clicked, that could be your issue.

Thanks again, the point was let reversecid = [...cid].reverse();
Now I just need to finish the code for tests 18 and 19