Building a cash register

so here is my code:

let cash = document.getElementById("cash");
let displayChangeDue = document.getElementById("change-due");
let purchaseBtn = document.getElementById("purchase-btn");

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]
];

function formatResults(status, change) {
  displayChangeDue.innerHTML = `<p>Status: ${status}</p>`;
  change.forEach(
    money => (displayChangeDue.innerHTML += `<p>${money[0]}: $${money[1]}</p>`)
  );
}

function checkRegister() {
  if (!cash.value) {
    return null;
  }

  let cashValue = parseFloat(cash.value);

  if (cashValue < price) {
    alert("Customer does not have enough money to purchase the item");
    cash.value = "";
    return;
  } else if (cashValue === price) {
    displayChangeDue.innerHTML = "<p>No change due - customer paid with exact cash</p>";
    cash.value = "";
    return;
  }

  let changeDue = cashValue - price;
  let result = { status: 'OPEN', change: [] };
  let reverseCid = [...cid].reverse();
  let denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
  let totalCid = cid.reduce((total, denom) => total + denom[1], 0);

  if (totalCid < changeDue) {
    displayChangeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>";
    return;
  }

  if (totalCid === changeDue) {
    result.status = 'CLOSED';
  }

  for (let i = 0; i < reverseCid.length; i++) {
    if (changeDue >= denominations[i] && changeDue > 0) {
      let count = 0;
      let total = reverseCid[i][1];
      while (total > 0 && changeDue >= denominations[i]) {
        total -= denominations[i];
        changeDue = parseFloat((changeDue - denominations[i]).toFixed(2));
        count++;
      }
      if (count > 0) {
        result.change.push([reverseCid[i][0], count * denominations[i]]);
      }
    }
  }

  if (changeDue > 0) {
    displayChangeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>";
    return;
  }

  formatResults(result.status, result.change);
}

purchaseBtn.addEventListener("click", checkRegister);

and these are the following errors:

FAILED: 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”.

FAILED: When the value in the #cash element is equal to price, the value in the #change-due element should be “No change due - customer paid with exact cash”.

FAILED: When price is less than the value in the #cash element, total cash in drawer cid is greater than the change due, individual denomination amounts allows for returning change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: OPEN” with required change due in coins and bills sorted in highest to lowest order.

FAILED: When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, individual denomination amounts make impossible to return needed change, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: INSUFFICIENT_FUNDS”

FAILED: When price is less than the value in the #cash element, total cash in drawer cid is less than the change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: INSUFFICIENT_FUNDS”.

FAILED: When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: CLOSED” with change due in coins and bills sorted in highest to lowest order.

Can someone help me, thanks!!

hi there, please include a link to the exercise to help people read your question in context.

There is a bug for this project right now. I would wait till it is fixed.

Oh okay thanks! Will move on to the other projects as well.

Here is the link, sorry for the inconvenience.

Hi lasjorg,

I see the PR has been merged, but has the fix made it into prod yet? I’m still getting [ReferenceError: _randomNumber is not defined]

when it is added to the prod-current branch it means it has been released

1 Like

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