Build a Cash Register Project works but fails test #8

I’m not sure why the test case - When the value in the #cash element is less than price , an alert should appear with the text "Customer does not have enough money to purchase the item" is not passing when this test case passes - When price is 20 and the value in the #cash element is 10 , an alert should appear with the text "Customer does not have enough money to purchase the item"

let price = 20;
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 cash = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const changeDue = document.getElementById('change-due');

function totalCid() {
  let res = 0;
  for (let i = 0; i < cid.length; i++) {
    res += Number(cid[i][1]);
  }
  return res.toFixed(2);
}

purchaseBtn.addEventListener('click', () => {
  let cidv = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  let change = (parseFloat(cash.value) - price).toFixed(2);
  let res = {status: 'Status: OPEN', change: ''};
  const updateRes = () => `${res.status} ${res.change}`;
  
  cidv.reverse();

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

  console.log(`${change} > ${totalCid()} = ${change > totalCid()}`);
  
  if (change > totalCid()) {
    res.status = 'Status: INSUFFICIENT_FUNDS';
    changeDue.textContent = res.status;
    return;
  } else if (change === totalCid()) {
    res.status = 'Status: CLOSED';
  }

  for (let i = 0; i < cid.length; i++) {
    while (cidv[i] <= change && cid[i][1] - cidv[i] >= 0) {
      change = (change - cidv[i]).toFixed(2);
      cid[i][1] = (cid[i][1] - cidv[i]).toFixed(2);
    }
  }

  console.log(cid);

  if (change > 0) {
    res.status = 'Status: INSUFFICIENT_FUNDS';
    changeDue.textContent = res.status;
    return;
  }
});

hello and welcome to fcc forum :slight_smile:

could you also share this step url as well, thanks and happy coding :slight_smile:

Hi and thank you! It’s the Build a Cash Register Project and the test number that’s working but not passing is test #8.

Nevermind I just ran the tests again and now everything is passing. Not sure what happened :confused:

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