Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

My code seemingly does all that I am asked to do. However It is failing for the open status code sections and closed status sections. My code reflects/matches what they want. I’m not sure what I’m missing here. Please Help?

Your code so far

<!-- file: index.html -->

let price = 0;
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]
];

// let cid = [
//   ["PENNY", 0.5], 
//   ["NICKEL", 0], 
//   ["DIME", 0], 
//   ["QUARTER", 0], 
//   ["ONE", 0], 
//   ["FIVE", 0], 
//   ["TEN", 0], 
//   ["TWENTY", 0], 
//   ["ONE HUNDRED", 0]
//   ];


const cash = document.getElementById("cash");
const cost = document.getElementById("price");
const purchaseBtn = document.getElementById("purchase-btn");
const registerDisplay = document.getElementById("change-due");
const drawer = document.getElementById("cid");
const menuBtns = document.querySelectorAll("#menu button");

let reverseCid = [...cid].reverse();
let values = [100, 20, 10, 5, 1, .25, .1, .05, .01];
let status = "";
let changeDisplay = [];
let totalCid = reverseCid.reduce((sum, coin) => sum + coin[1], 0);
let display = {};

const cashValue = () => +(+cash.value).toFixed(2);
const changeDue = () => +(cashValue() - price).toFixed(2);

const cashRegister = () => {
  registerDisplay.innerHTML = "";
  display = {};
  changeDisplay = [];
  drawerLogic();
  updateUI();
  cash.value = "";
}

const drawerLogic = () => {
  let change = changeDue();
  changeDisplay = [];
  let totalCidAvailable = totalCid;

  if (cashValue() < price) {
    alert("Customer does not have enough money to purchase the item");
    return;
  }

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

  

  for (let i = 0; i < reverseCid.length; i++) {
    const coinName = reverseCid[i][0];
    let coinValue = values[i];
    let coinAmount = reverseCid[i][1];
    
    let changeGiven = 0; 
  
    while (change >= coinValue && coinAmount > 0) {
      change -= coinValue;
      change = +change.toFixed(2);
      coinAmount -= coinValue;
      changeGiven += coinValue;
    }

    if (changeGiven > 0) {
      changeDisplay.push([coinName, +changeGiven.toFixed(2)]);
      reverseCid[i][1] = coinAmount;
    }
  }

  for (let i = 0; i < reverseCid.length; i++) {
    reverseCid[i][1] = Math.max(0, reverseCid[i][1]);
  }

  let totalReturnedChange = changeDisplay.reduce((sum, coin) => sum + parseFloat(coin[1]), 0);
  
  if (totalReturnedChange < change) {
    status = "INSUFFICIENT_FUNDS";
  } else {
    if (totalCidAvailable === 0 || (totalCidAvailable === totalCid && totalReturnedChange >= totalCid)) {
      status = "CLOSED";
    } else {
      status = "OPEN";
    }
  }

  updateDrawer();
}

const updateDrawer = () => {
  drawer.innerHTML = reverseCid.map(([name, value]) => `<span>${name}: <strong>${value.toFixed(2)}</strong> </span>`).join("");
}

cost.textContent = `$${price.toFixed(2)}`;

drawer.innerHTML = reverseCid.map(([name, value]) => `<span>${name}: <strong>${value.toFixed(2)}</strong> </span>`).join("");


const updateUI = () => {

  if (cashValue() <= 0) {
    return;
  }

  for (let i = 0; i < changeDisplay.length; i++) {
    let coin = changeDisplay[i][0];
    let value = changeDisplay[i][1];

    if (!display[coin]) {
      display[coin] = 0;
    }
    display[coin] += +value;
  }

 if (status === "INSUFFICIENT_FUNDS") {
   registerDisplay.innerHTML += `<p>Status: ${status}</p>`;
 } else if (status && cashValue() !== price) {
   registerDisplay.innerHTML += `<p>Status: ${status} ${Object.keys(display).map(coin => `${coin}: $${display[coin]}`).join(" ")}</p>`;
 }
}

menuBtns.forEach(button => {
    button.addEventListener("click", () => {
        cost.textContent = "";
        price = parseFloat(button.value);
        cost.textContent = `$${price.toFixed(2)}`
    });
});

purchaseBtn.addEventListener("click", cashRegister);
cash.addEventListener("keydown", (event) => {
  if (event.key === "Enter") {
    cashRegister();
  }
})

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Hey there,

Please update the message to include your code. The code was too long to be automatically inserted by the help button.

When you enter a code, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

oof! I think it should be there now!

I think the problem is in the drawerLogic somewhere but I’m not sure what I’m looking for since it seems to do what the test wants.

Code like the above is declared in the global scope and will cause the tests to fail.

All code that handles cid or change-due or price needs to be declared in a function that runs when the purchase button is clicked.
Please re-write your logic to not rely on global values except for the ones originally given (price and cid).

1 Like

A million thank you’s!!! I’ve been working on this for sooooo long and never would have realized. :smiling_face_with_three_hearts:

note: This also pointed out a flaw in my logic that I had previously missed.

what a life saver!

1 Like