Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I have gone through the steps but not able to pass the last 2 steps and I am confused where the problem is.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Cash Register</title>
    <link rel="stylesheet" href="./styles.css" />
  </head>
  <body>
    <main>
<input id="cash">
<div id="change-due">
  </div>
<button id="purchase-btn">Sale
  </button>
  
    <script src="./script.js"></script>
  </body>
</html>
/* file: script.js */
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]
];

const clientCash = document.getElementById("cash");
const clientChange = document.getElementById("change-due");
const clientSale = document.getElementById("purchase-btn");

let currencyUnits = [
  ['PENNY', 0.01],
  ['NICKEL', 0.05],
  ['DIME', 0.1],
  ['QUARTER', 0.25],
  ['ONE', 1],
  ['FIVE', 5],
  ['TEN', 10],
  ['TWENTY', 20],
  ['ONE HUNDRED', 100]
];

clientSale.addEventListener("click", () => {
const cashValue=parseFloat(clientCash.value);
const changeDue=cashValue-price;

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

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

 const changeResult = getChange(changeDue, cid);
 if (changeResult.status === "INSUFFICIENT_FUNDS" || changeResult.status === "CLOSED") {clientChange.innerText = `Status: ${changeResult.status} 
 ${formatChange(changeResult.clientChange)}`}
 else {
   let changeText = `Status: OPEN ${formatChange(changeResult.clientChange)}`;
   clientChange.innerText = changeText;
 }
});

const getChange = (changeDue, cid) => {
  let totalCid = parseFloat(cid.reduce((sum, [_,amount])=> sum + amount, 0).toFixed(2))
if (totalCid < changeDue) {return { status: "INSUFFICIENT_FUNDS", clientChange:[]}}

//iterate through the currencyUnits backward
let changeArray = [];
let remainingChange = changeDue;

for (let i = currencyUnits.length-1; i >= 0; i--) {
  let unit = currencyUnits[i][0];
  //accessing the array within the the currencyunits array(like an array within array)[i]represents the 8th array ['ONE HUNDRED', 100] & [0] represents the first index 'ONE HUNDRED' of the 8th array

  let unitValue = currencyUnits[i][1];
  //accessing the array within the the currencyunits array(like an array within array)[i]represents the 8th array ['ONE HUNDRED', 100] & [1] represents the 2nd index 100 of the 8th array. NB: this is going to loop through all backward

  let unitInDrawer = cid[i][1];
  ////accessing the array within the the cid array(like an array within array)[i]represents the 8th array ['ONE HUNDRED', 100] & [1] represents the 2nd index 100 of the 8th array. NB: this is going to loop through all backward

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

  while (remainingChange >= unitValue && unitInDrawer > 0) {
    remainingChange = parseFloat((remainingChange - unitValue).toFixed(2));
    unitInDrawer -= unitValue;
    amountFromUnit += unitValue;
  }


  if (amountFromUnit > 0) {
    changeArray.push([unit, amountFromUnit]);
    cid[i][1] -= amountFromUnit;
  }
}
}// end of for loop

if (remainingChange > 0) {
  return { status: "INSUFFICIENT_FUNDS", clientChange:[]}
}

if (changeDue === totalCid) {
  return { status: "CLOSED", clientChange: cid}
}

return { status: "OPEN", clientChange: changeArray}

}

// end of getChange ()

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

//console.log(formatChange())
/* file: styles.css */

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You can find a repo of code that simulates the tests here. Add this to the bottom of your script file then check the console output to see actual versus expected.