Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

As far as I have checked my code is handling all the problem correctly but still, I am failing the cases mentioned.

Your code so far

const calculateChange=(paid,due)=>{
    let change=paid-due
    const currency=[
        ['PENNY',0.01,1.01],
        ["NICKEL",0.05,2.05],
        ["DIME",0.10,3.1],
        ["QUARTER",0.25,4.25],
        ["ONE",1,90],
        ["FIVE",5,55],
        ["TEN",10,20],
        ["TWENTY",20,60],
        ["ONE HUNDRED",100,100]
      ]
    const reversedCurrency = currency.reverse();
    var curr_arr=reversedCurrency.reduce(function(acc,curr){
        let value=0;
        while (curr[2] > 0 && change >= curr[1]) {
            change = parseFloat((change - curr[1]).toFixed(2));
          curr[2] -= curr[1];
          value += curr[1];
      }
      if (value > 0) {
        acc.push([ curr[0], value ]);
      }
      return acc;
      },[])
    return curr_arr
}

purchaseBtn.addEventListener("click",()=>{
    let paid=parseFloat(cash.value,2);
    let due=parseFloat(totalAmountDue.textContent).toFixed(2);
    let total = parseFloat(cid.reduce((sum, currency) => sum + currency[1], 0)).toFixed(2);
    if(paid===due){
        displayChangeDue.style.display="block"
        displayChangeDue.innerHTML=`<p> No change due - customer paid with exact cash </p>`
    }else if(paid<due){
        alert("Customer does not have enough money to purchase the item");

    }else if((paid-due)>total){
        displayChangeDue.style.display="block"
        displayChangeDue.innerHTML=`<p> Status: INSUFFICIENT_FUNDS </p>`
    }else{
        let change=calculateChange(paid,due);
     if(total===(paid-due).toFixed(2)){
         displayChangeDue.style.display="block";
    displayChangeDue.innerText=`Status: CLOSED`
     }   
    else{
       displayChangeDue.style.display="block";
    displayChangeDue.innerText=`Status: OPEN` 
    }
    change.forEach(i => {
  displayChangeDue.innerText += ` ${i[0]}: $${i[1].toFixed(2)}`;
});
}
})

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

it is failing cases
9. When price is 11.95 and the value in the #cash element is 11.95, the value in the #change-due element should be “No change due - customer paid with exact cash”.

  1. When the value in the #cash element is equal to price, the value in the #change-due element should be “No change due - customer paid with exact cash”.

  2. When price is 3.26, the value in the #cash element is 100, cid is [[“PENNY”, 1.01], [“NICKEL”, 2.05], [“DIME”, 3.1], [“QUARTER”, 4.25], [“ONE”, 90], [“FIVE”, 55], [“TEN”, 20], [“TWENTY”, 60], [“ONE HUNDRED”, 100]], and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04”.

  3. When price is less than the value in the #cash element, total cash in drawer cid is greater than the change due, individual denomination amounts allows for returning change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: OPEN” with required change due in coins and bills sorted in highest to lowest order.

  4. When price is 19.5, the value in the #cash element is 20, cid is [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“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: INSUFFICIENT_FUNDS”.

  5. When price is less than the value in the #cash element, total cash in drawer cid is greater than change due, but the individual denomination amounts make it impossible to return needed change, when the #purchase-btn element is clicked, the value in the #change-due element should be “Status: INSUFFICIENT_FUNDS”

  6. 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”.

  7. When price is less than the value in the #cash element, total cash in drawer cid is equal to change due, and the #purchase-btn element is clicked, the value in the #change-due element should be “Status: CLOSED” with change due in coins and bills sorted in highest to lowest order.

The original script.js file contained two given global variables, price and cid. I don’t see either one of those in your code. The tests use those variables and change them to handle different scenarios.

I have them but is it because I am not using these variables in my code that I am facing the issue?
This is the rest of Js that I didnt mention
let price = 1.87;
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 displayChangeDue = document.getElementById(‘change-due’);
const cash = document.getElementById(‘cash’)
const purchaseBtn = document.getElementById(‘purchase-btn’);
const priceScreen = document.getElementById(‘price-screen’);
const cashDrawerDisplay = document.getElementById(‘cash-drawer-display’);
const totalAmountDue=document.getElementById(‘total’);

Yes.

If you’d like to test your application with different values for price and cid, make sure to declare them with the let keyword so they can be reassigned by our tests.

The test changes those variables, so if your code doesn’t use them, the tests will not work.