Javascript Challenge - Cash Register

Hi,

I am currently experiencing issues with the tests with

Javascript Challenge - Cahs Register

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body>
    <input id="cash" type="text"/>
    <div id="change-due"></div>
    <button id="purchase-btn">Purchase</button>
  </body>
  <script src="./script.js"></script>
</html>

JS


let price;
let cid;


// Tests 11-13 require this to pass, 13 ocasionally does not pass but it passes after retesting a few times
/*
price = 3.26;
cid = [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]];
*/

// Test 14-17 require this to pass
/*
price=19.5;
cid = [["PENNY", 0.01], ["NICKEL",    0], ["DIME", 0  ], ["QUARTER",    0], ["ONE",  0], ["FIVE",  0], ["TEN",  0], ["TWENTY",  0], ["ONE HUNDRED",   0]];
*/

// Tests 18-19 dont pass no matter what, even though it works in live
price=19.5;
cid = [["PENNY", 0.5 ], ["NICKEL",    0], ["DIME", 0  ], ["QUARTER",    0], ["ONE",  0], ["FIVE",  0], ["TEN",  0], ["TWENTY",  0], ["ONE HUNDRED",   0]];

// CID1 IS NOT USABLE | we need exact number of bills and coins
// Remapping to a usable Array

const currency = {
  'PENNY': 0.01,
  'NICKEL': 0.05,
  'DIME': 0.1,
  'QUARTER': 0.25,
  'ONE': 1,
  'FIVE': 5,
  'TEN': 10,
  'TWENTY': 20,
  'ONE HUNDRED': 100,
}

let cashRegister = cid.map((drawer)=>{
  return (
    { 
      currencyName:   drawer[0],                                  
      currencyValue:  currency[drawer[0]],                      
      currencyAmount: (drawer[1]*1000)/(currency[drawer[0]]*1000) 
    }
  )
}).sort((a,b)=> {
  return b.currencyValue - a.currencyValue  
} )


let cash        = document.querySelector("#cash");
let purchaseBtn = document.querySelector("#purchase-btn");
let changeDue   = document.querySelector("#change-due");

purchaseBtn.addEventListener("click",()=>{

  if(!cash.value.length>0){return;}

  const customerAmount = Number.parseFloat(cash.value)

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

  if(customerAmount === price){
    changeDue.innerText="No change due - customer paid with exact cash";
    return;
  }

  let change = (customerAmount*1000-price*1000)/1000
  let text = ""


  // Create a clone of current state. Dont use the live state
  const cashRegisterUpdated = [...cashRegister]

  // Attempt Transaction
  for(let i=0;i<cashRegisterUpdated.length;i++){

    const drawer = cashRegisterUpdated[i];
    
    let {currencyName, currencyValue,currencyAmount} = drawer

    // Currency Type Used
    if(change>=currencyValue && currencyAmount >0){ text+=" " + currencyName + ": $" }

    let withdrawnCurrencyAmount=0 //Required For Status Message
    while(
      1 === 1
      && cashRegisterUpdated[i].currencyAmount > 0       //We have Bills/Coins
      && change>=currencyValue                           //We can exchange the Bills/Coins
    ){
      cashRegisterUpdated[i].currencyAmount-=1        //Remove 1 Bill / Coin
      change=(change*1000-currencyValue*1000)/1000;   //Update remaining change
      withdrawnCurrencyAmount++
    }

    // Compute Total Amount Withdrawn for that Currency and add to Status
    if(withdrawnCurrencyAmount>0){
      text+=withdrawnCurrencyAmount * currencyValue
    }

  }

  // Update Status INSUFFICIENT_FUNDS
  if(change>0){
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS"
    return;
  }

  // Commit Transaction
  cashRegister = [...cashRegisterUpdated ]

  // Update Status CLOSED
  const registerHasCash = cashRegister.filter((drawer) => drawer.currencyAmount>0).length > 0
  if(registerHasCash === false){
    changeDue.innerHTML = "Status: CLOSED" + text
    return;
  }
  
  // Update Status OPEN
  changeDue.innerHTML = "Status: OPEN" + text
 
})

Please assist

don’t do this, any calculations that you do in the global scope works only when the app starts for the first time, and are not run again when the tests change cid and price

1 Like

That was it, the moment I moved it on the ClickEvent, all tests went green. Thanks