Cash register project

somehow i cant seem to pass the last test with the condition block stated i still have it at status OPEN

//declare 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]];
//let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
//let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const cashRegisterChecker = () => {

  //variables for cash/change/totalcid
  let cash = Number(cashInput.value);
  let change = Math.round(cash * 100) - Math.round(price * 100);
  console.log(cash, price)
  cid = cid.reverse();
  //console.log(cid)
  let totalCID = 0;
  for(let i = 0; i < cid.length; i++){
    totalCID += cid[i][1];
    //console.log(totalCID)
  }
  //console.log(totalCID)

  //1st condition check
  if(cash < price){
    alert("Customer does not have enough money to purchase the item")
  } else if (cash === price){
    changeDue.innerHTML = "No change due - customer paid with exact cash"
    return
  } else if (totalCID < change){
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS"
  }

  //declare denomination
  let denomination = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1]
  //console.log(denomination)
  //empty variable to store changeArr result from loop
  let changeArr = [];
  //console.log(cid)

  for(let i = 0; i < cid.length; i++){
    if(change > denomination[i] && change > 0){
    let result = 0;
    let cidValue = Math.round(cid[i][1] * 100);
    let cidName = cid[i][0];
    //console.log(cidValue)
    //console.log(change, denomination[i])
    //console.log(denomination)
    //console.log(result)
    while (change >= denomination[i] && cidValue > 0){
      change -= denomination[i];
      //console.log(result)
      cidValue -= denomination[i];
      result += denomination[i];
     //console.log(result)
    }
    if (result > 0){
      changeArr.push(`${cidName}: $${result / 100}`)
      
    }
  
  } 

  if(change < totalCID){
    changeDue.textContent = `"Status: OPEN ${changeArr}"`
  } else if (totalCID === change){
    changeDue.textContent = `"Status: CLOSED ${changeArr}"`
    return;
  } 
}


}

purchaseBtn.addEventListener("click", cashRegisterChecker)

please provide the link to the project so that we can test. Also please provide your html so that we can debug

Noted, this is the link copy.

My html

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Cash Register</title>
  </head>

  <body>

    <h1>Cash Register</h1>

    <div id="change-due"></div>

    <div id="container">
      <label for="cash">Cash from customer</label>
      <br>
      <input type="number" id="cash">
      <br>
      <button id="purchase-btn">Purchase</button>
    </div>

    <div id="container2">
      <p id="price"></p>      
    </div>

    <script src="script.js"></script>
  </body>

</html>

my script

//Import from html
const cashInput = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");
const priceTotal = document.getElementById("price");

//declare 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]];
//let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
//let cid = [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
//let cid = [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];

const cashRegisterChecker = () => {

  //variables for cash/change/totalcid
  let cash = Number(cashInput.value);
  let change = Math.round(cash * 100) - Math.round(price * 100);
  console.log(cash, price)
  cid = cid.reverse();
  //console.log(cid)
  let totalCID = 0;
  for(let i = 0; i < cid.length; i++){
    totalCID += cid[i][1];
    //console.log(totalCID)
  }
  //console.log(totalCID)

  //1st condition check
  if(cash < price){
    alert("Customer does not have enough money to purchase the item")
  } else if (cash === price){
    changeDue.innerHTML = "No change due - customer paid with exact cash"
    return
  } else if (totalCID < change){
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS"
  }

  //declare denomination
  let denomination = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1]
  //console.log(denomination)
  //empty variable to store changeArr result from loop
  let changeArr = [];
  //console.log(cid)

  for(let i = 0; i < cid.length; i++){
    if(change > denomination[i] && change > 0){
    let result = 0;
    let cidValue = Math.round(cid[i][1] * 100);
    let cidName = cid[i][0];
    //console.log(cidValue)
    //console.log(change, denomination[i])
    //console.log(denomination)
    //console.log(result)
    while (change >= denomination[i] && cidValue > 0){
      change -= denomination[i];
      //console.log(result)
      cidValue -= denomination[i];
      result += denomination[i];
     //console.log(result)
    }
    if (result > 0){
      changeArr.push(`${cidName}: $${result / 100}`)
      
    }
  
  } 

  if(change < totalCID){
    changeDue.textContent = `"Status: OPEN ${changeArr}"`
  } else if (totalCID === change){
    changeDue.textContent = `"Status: CLOSED ${changeArr}"`
    return;
  } 
}


}

purchaseBtn.addEventListener("click", cashRegisterChecker)

Try this:

write 100 in the input element. Then press multiple times the button.
You get two outputs alternating:
This, which is correct "Status: OPEN TWENTY: $60,TEN: $20,QUARTER: $0.5"

but also
"Status: OPEN NICKEL: $20,DIME: $10,QUARTER: $5,ONE: $45,FIVE: $0.5"

which has impossible values, like FIVE: $0.5
but also values that are much higher than what is available in cid.

It looks like your code is using global variables that are not reset between each press of the button.

okay will fix the problem on that. Thanks :smile:

You should remove the quotes from around the output

you should not do things in the global environment

alright noted and thanks for the help :smiley: