Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I can not pass the last two tests no matter how much i change my code

Your code so far

<!-- file: index.html -->
<html>
  <body>
    <input id="cash" >
    
    <button id="purchase-btn" >change</button>

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

    <script src="script.js"></script>
  </body>
</html>
/* file: styles.css */

/* file: script.js */
let price = 19.5;
let cid = [
  ["PENNY", .5],
  ["NICKEL", 0], 
  ["DIME", 0], 
  ["QUARTER", 0], 
  ["ONE", 0], 
  ["FIVE", 0], 
  ["TEN", 0], 
  ["TWENTY", 0], 
  ["ONE HUNDRED", 0]
];

const cash = document.getElementById('cash');
const change = document.getElementById('change-due');
const button = document.getElementById('purchase-btn');
 
let currencyUnits = [
  ['PENNY', .01],
  ['NICKEL', .05],
  ['DIME', .1],
  ['QUARTER', .25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
]; 





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

const cashValue = parseFloat(cash.value);
const changeDue = cashValue - price;

  if(cashValue < price){
  alert("Customer does not have enough money to purchase the item");
  return;
} 
 if (cashValue === price){
  change.innerText = "No change due - customer paid with exact cash";
  return;
} 

const changeResult = getChange(changeDue, cid);

if(changeResult.status === "INSUFFICIENT_FUNDS"){
  change.innerText = `Status: ${changeResult.status} ${formatChange(changeResult.change)}`
} else if(changeResult.status === "CLOSED"){
  change.innerText = `Status: ${changeResult.status} ${formatChange2(changeResult.change)}`
} else {
  let changeText = `Status: OPEN ${formatChange2(changeResult.change)}`
  change.innerText = changeText.trim();
}

});


const getChange = (changeDue,changeAvailable)=>{
  let totalCid= parseFloat(changeAvailable.reduce((acc,[x,y])=> acc + y, 0).toFixed(2));
   if(totalCid < changeDue){
     return {status: "INSUFFICIENT_FUNDS", change: []}
   }

   let changeArray = [];
   let remainingChange = changeDue;

  for(let i = 8;i >= 0;i--){
    let unit = currencyUnits[i][0];
    let unitValue = currencyUnits[i][1];
    let unitInTill = cid[i][1];

    if(unitValue < remainingChange && unitInTill > 0){
      let amountFromUnit = 0;

      while(remainingChange >= unitValue && unitInTill > 0){
        remainingChange = (remainingChange - unitValue).toFixed(2);
        unitInTill -= unitValue;
        amountFromUnit += unitValue;
 
      }
      if(amountFromUnit > 0){
        changeArray.push([unit,amountFromUnit])
      }
    }
  }
  if(remainingChange > 0){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }

  if(changeDue === totalCid){
    return {status: "CLOSED", change: cid}
  } //else if(){}
  return {status: "OPEN", change: changeArray}
}
 
const formatChange = changeArray => changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).join(' ');

const formatChange2 = changeArray => changeArray.map(([unit, amount]) => `${unit}: $${amount.toFixed(2)}`).reverse().join(' ');


Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You can add these scripts to the bottom of your js file to compare what your code is returning to what is expected to help you troubleshoot.

Thank you for the help but i still couldn’t figure it out

What is the output in your console?

// running tests
18. 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”.
19. 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.
// tests completed

the output from the scripts, not the tests

Thank you! Yes, that’s what I was asking for.

Test #9
actual No change due - customer paid with exact cash
expected No change due - customer paid with exact cash

Test #11
actual Status: OPEN QUARTER: $0.50
expected Status: OPEN QUARTER: $0.5

Test #12
actual Status: OPEN PENNY: $0.04 DIME: $0.20 QUARTER: $0.50 ONE: $1.00 FIVE: $15.00 TEN: $20.00 TWENTY: $60.00
expected Status: OPEN TWENTY: $60 TEN: $20 FIVE: $15 ONE: $1 QUARTER: $0.5 DIME: $0.2 PENNY: $0.04

Test #14
actual Status: INSUFFICIENT_FUNDS
expected Status: INSUFFICIENT_FUNDS

Test #16
actual Status: INSUFFICIENT_FUNDS
expected Status: INSUFFICIENT_FUNDS

Test #18
actual Status: CLOSED ONE HUNDRED: $0.00 TWENTY: $0.00 TEN: $0.00 FIVE: $0.00 ONE: $0.00 QUARTER: $0.00 DIME: $0.00 NICKEL: $0.00 PENNY: $0.50
expected Status: CLOSED PENNY: $0.5

you may notice the difference here