Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

when you change the cid, different tests change, replace this cid with these 2 cids to understand more, then run tests, you will notice how some tests fail and other success

Your code so far

let price = 1.87;
// Here is the cid, change it for more info
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]
];
cid.forEach(arr => drawer.innerHTML += `<br>${arr[0]}: $${arr[1]}`)
let reversedCid = cid.slice().reverse();
const purchaseBtn = document.getElementById('purchase-btn');
//to log anything to console easier using log();
const log = arg => console.log(arg);
const priceDiv = document.getElementById('price-div');
let changeDue = document.getElementById('change-due');
const fix = arg => parseFloat((arg).toFixed(2));
const cashEl = document.getElementById('cash');
log(fix(2.1 * 100))
priceDiv.textContent = `price : $${price}`
function checkCashRegister(){
    let cash = parseFloat(document.getElementById('cash').value);
  if(cash < price){
    alert("Customer does not have enough money to purchase the item");
  }else if(cash == price){
    changeDue.textContent = "No change due - customer paid with exact cash";    
  }else{ 
    let x = JSON.parse(JSON.stringify(reversedCid));
    const amount =[100,20,10,5,1,0.25,0.1,0.05,0.01]; 
   
    let fixedCash = fix(cash - price);
    let due = [ ["ONE HUNDRED", 0],
                ["TWENTY", 0],
                ["TEN", 0],
                ["FIVE", 0],
                ["ONE", 0],
                ["QUARTER", 0],
                ["DIME", 0],
                ["NICKEL", 0],
                ["PENNY", 0]
    ]; 
    amount.forEach((num, index) => {
      while (fixedCash >= num && reversedCid[index][1] >= num) { 
        fixedCash = fix(fixedCash - num);
        due[index][1] = fix(due[index][1] + num);
        reversedCid[index][1] = fix(reversedCid[index][1] - num);
      }
    });
    
    if (fixedCash !== 0) {
      log(cid)
      reversedCid = x;
      fixedCash = fix(cash - price);
      changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
    } else {
      cid = [["PENNY", reversedCid[8][1]],
             ["NICKEL", reversedCid[7][1]],
             ["DIME", reversedCid[6][1]],
             ["QUARTER", reversedCid[5][1]],
             ["ONE", reversedCid[4][1]],
             ["FIVE", reversedCid[3][1]],
             ["TEN", reversedCid[2][1]],
             ["TWENTY", reversedCid[1][1]],
             ["ONE HUNDRED", reversedCid[0][1]
      ]];
      let cidSum = 0;
      cid.forEach(num => cidSum += num[1])             
      drawer.innerHTML = '<b>changes in cid:</b>';
      cid.forEach(arr => drawer.innerHTML += `<br>${arr[0]}: $${arr[1]}`)
      due = due.filter(num => num[1] !== 0)
      changeDue.textContent = cidSum !== 0 ? "Status: OPEN" : "Status: CLOSED";
      due.forEach(arr => changeDue.innerHTML += `<br>${arr[0]}: $${arr[1]}`)  
    }
  }
}
purchaseBtn.addEventListener('click',checkCashRegister)
cashEl.addEventListener('keydown',k=>{
if(k.key === 'Enter'){
 checkCashRegister();
}
})

Use this cid value

[["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]

then run the tests
then use this one

[["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]

Then run tests
I think problem in free code camp variable test

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0

Challenge Information:

Build a Cash Register Project - Build a Cash Register

Did you have a question?