Cash Register Project

This is my code, i think it does everything it needs to do, but I don’t pass… Can someone have a look at it? Thanks! They ask: if price = 19.5 and cash = 20, the value in changeDue needs to be: Status: OPEN QUARTER: $0.5
It is exactly that

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 valutaValue = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.1],
  ['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 purBtn = document.getElementById("purchase-btn");

const checkBeforePurchase = () => {
  
}
//calculate change
cid.reverse()
valutaValue.reverse();

const calculateChange = () => {

  let changeAmount = cash.value - price;
  let changeArr = [];
  let insufficientFunds = false;

  for (let i = 0; i < cid.length; i++){
    let coinName = valutaValue[i][0];
    let coinValue = valutaValue[i][1];
    let coinAvailable = cid[i][1];
    let amountNeeded = 0;

    while (changeAmount >= coinValue && coinAvailable > coinValue){
      changeAmount -= coinValue;
      changeAmount = Number(changeAmount.toFixed(2));
      coinAvailable -= coinValue;
      coinAvailable = Number(coinAvailable.toFixed(2));
      amountNeeded += coinValue;
      amountNeeded = Number(amountNeeded.toFixed(2));
    }
    cid[i][1] = coinAvailable;

    if (amountNeeded > 0) {
      changeArr.push(`${coinName}: \$${amountNeeded}`);
    }

    if (changeAmount > 0 && coinAvailable < coinValue) {
      insufficientFunds = true; 
    }
  }

  if (changeAmount === 0 && !insufficientFunds) {
    changeDue.textContent = `Status: OPEN ${changeArr.join(" ")}`;
  } else {
    changeDue.textContent = `Status: OPEN INSUFFICIENT_FUNDS`;
  }
  if (cash.value < price){
    alert("Customer does not have enough money to purchase the item")
  } if (cash.value == price){
    changeDue.textContent = "No change due - customer paid with exact cash";
  }
  
  cash.value = "";
}

//Check all functions
const allFunctions = (e) => {
  e.preventDefault

  calculateChange();
}

purBtn.addEventListener("click", allFunctions);

This code and any other code you wrote (other than the price and cid and nonfunctional definitions) that is not in a function triggered by the purchase button will cause the tests to fail because they load your code once only and then never again for each test. (so any code in the global scope that was not given to you should almost always be in a function triggered by the purchase click).

Alright, but when I put it inside the function, it reverses everytime I press the purBtn. Is there a way to not have that? Or place reverse again at the end of the scope?

I’m not sure why reversing when the purchase button is clicked is a problem?
you can also just read the array in reverse.

1 Like

When I press the purBtn the array gets reversed, when I press again, the reversed array will be reversed again, so it uses the “normal” array in the calculations, which gives the wrong changeDue output

as I said, you also have the option of just reading the array in reverse
or even making a copy of it locally to reverse and leave the cid unreversed.
I’m sure you can work this out.

1 Like

Ahhh you mean like declaring a let rcid = cid.splice().reverse() to use or don’t do that at all and just everytime I use cid, I use cid.reverse() instead. So the array don’t get reversed, but it does in those certain calculations?

yes you can reverse a copy of the array or read it backwards.
(I would research more how to reverse an array as I don’t think splice is going to help you)
You can google that if you forgot how.

1 Like

Thanks a lot, i used slice() to make a shallow copy of the array, but when I declare rcid = cid.reverse() I get the same result. So that was a little stupid from me, Sometimes we think to difficult :sweat_smile: I’m reading right now about it, and I understand what you mean. Thanks!

1 Like