Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I’ve gotten my code to the point where it passes all tests except the last one.

" When price is 19.5, the value in the #cash element is 20, cid is [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]], and the #purchase-btn element is clicked, the value in the #change-due element should be "Status: CLOSED PENNY: $0.5""

I’ve been at it for hours and can’t seem to figure out what’s wrong.

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

// freeCodeCamp provided variables
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],
];

// Declare Global Variables
const cash = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");

// Results Function
const changeDueResults = () => {
  checkRegister();
};

// Button event listener
purchaseBtn.addEventListener("click", changeDueResults);

// Function to round to nearest two decimal places
function roundToTwoDecimalPlaces(num) {
  return Math.round(num * 100) / 100;
}

// Check Register Function
const checkRegister = () => {
  let changeDueValue = parseFloat(cash.value) - price;
  let higherDenomFirst = cid.slice().reverse();
  let currencyUnits = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
  let status = { status: "OPEN", change: [] };
  let totalCurrency = 0;

  for (const total of cid) {
    totalCurrency += total[1];
  }

  if (parseFloat(cash.value) < price) {
    alert("Customer does not have enough money to purchase the item");
    cash.value = "";
    return;
  }

  if (parseFloat(cash.value) === price) {
    changeDue.innerHTML = `<p>No change due - customer paid with exact cash</p>`;
    cash.value = "";
    return;
  }

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

  let i = 0;
  outerLoop: while (i < higherDenomFirst.length) {
    let count = 0;
    let total = higherDenomFirst[i][1];

    while (total > 0 && changeDueValue >= currencyUnits[i]) {
      total -= currencyUnits[i];
      changeDueValue = roundToTwoDecimalPlaces(
        changeDueValue - currencyUnits[i],
      );
      count++;
    }

    const changeAmount = count * currencyUnits[i];
    if (changeAmount <= higherDenomFirst[i][1]) {
      status.change.push([higherDenomFirst[i][0], changeAmount]);
    } else {
      // Adjust changeDueValue and retry with the next denomination
      changeDueValue = roundToTwoDecimalPlaces(changeDueValue + changeAmount);
      i++;
      continue outerLoop;
    }

    i++;
  }

  if (changeDueValue > 0) {
    // Check if remaining change can be handled with PENNY
    if (changeDueValue <= cid[0][1]) {
      // Display as CLOSED
      changeDue.innerHTML = `<p>Status: CLOSED PENNY: $${changeDueValue.toFixed(2)}</p>`;
    } else {
      // Display as INSUFFICIENT_FUNDS
      changeDue.innerHTML = "<p>Status: INSUFFICIENT_FUNDS</p>";
    }
    return;
  }

  // Display the result
  const formattedChange = status.change
    .filter(([_, amount]) => amount > 0)
    .map(formatCurrency)
    .join(" ");

  if (formattedChange.trim() === "" && changeDueValue === 0) {
    // Use toFixed(2) to ensure proper rounding
    changeDue.innerHTML = `<p>Status: CLOSED PENNY: $${changeDueValue.toFixed(2)}</p>`;
  } else {
    changeDue.innerHTML = `<p>Status: OPEN ${formattedChange}</p>`;
  }
};

// Format currency function
function formatCurrency(money) {
  return money[1] > 0 ? `${money[0]}: $${money[1]} ` : "";
}

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

what is your html? that is also needed for debugging

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