Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

I’ve written the code. It is producing results as expected but the last two tests (18 and 19) refused to pass even when technically correct. Any assistance will be greatly appreciated.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width initial-scale=1.0" >
  <link rel="stylesheet" href="styles.css" >
  <title>Cash Register App</title>
</head>
<body>
  <h1>Cash Register Project</h1>
  <div id="form-display">
    <div id="change-display">
      <p id="change-due"></p>
    </div>
    <p>Enter cash from customer:</p>
    <input type="number" id="cash" ></input>
    <button type="submit" id="purchase-btn">Purchase</button>
  </div>
  <div id="total-frame">
    <div id="total" > </div>
  </div>  
  <div id="calculation-display">
    <div id="neck"></div>
    <div id="body">
      <div id="calculator">
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
        <div id="grid"></div>
      </div>
      <div id="drawer-status">
        <p><strong>Change in Drawer: </strong></p>
        <p>Pennies: <span id="0"></span></p>
        <p>Nickels: <span id="1"></span></p>
        <p>Dimes: <span id="2"></span></p>
        <p>Quarters: <span id="3"></span></p>
        <p>Dollars: <span id="4"></span></p>
        <p>Fives: <span id="5"></span></p>
        <p>Tens: <span id="6"></span></p>
        <p>Twenties: <span id="7"></span></p>
        <p>Hundreds: <span id="8"></span></p>
      </div>
    </div>
    <div id="bottom">
      <div id="circle"></div>
    </div>
  </div>
  <script src="script.js"></script>
</body>
</html>

/* file: script.js */
let price = 19.5;
let cid = [
  ['PENNY', 0.5],
  ['NICKEL', 0],
  ['DIME', 0],
  ['QUARTER', 0],
  ['ONE', 0],
  ['FIVE', 0],
  ['TEN', 0],
  ['TWENTY', 0],
  ['ONE HUNDRED', 0]
];
const amount = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];


document.getElementById("total").textContent = `Total: $${price}`;
const input = document.getElementById("cash");
const changeDue = document.getElementById("change-due");
const purchaseBtn = document.getElementById("purchase-btn");


const display = (bal, total) => { 
  //alert()
  if(bal > total){
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
  } else if(bal === total){
    changeDue.innerHTML = "Status: CLOSED";
    calculate(bal);
  } else if(bal < total){
    changeDue.innerHTML = "Status: OPEN";
    calculate(bal);
  }
}

let total = 0;
const cidTotal = () => {
  for(let k = 0; k < cid.length; k++){
    total = (total*100 + cid[k][1]*100)/100;    
  }   
}


const calculate = (bal) => {
  //console.log(cid)
  if(bal>0){
    getIndex(bal);
    //console.log(j)
    //console.log(cid)
    //balSort(bal, cid, j)
  }  
}

const loop = () =>{
  if(cid[j][1] <= 0 && j!= 0){
    j -= 1;
    loop()       
  } else {
    return j;
  }
}


let j = 0;
const getIndex = (bal) => {    
  for(let i = 0; i < amount.length; i++){
    //console.log(bal)
    if(bal === amount[i]){
      j = i;
      loop()
      balSort(bal, j)
    }
    if(bal < amount[i] && bal > amount[i-1]){
      j = i-1;
      loop()
      balSort(bal, j);
    }
  } 
  
};

const balSort = (bal, j) => {
  const levelBal = Math.floor(bal/amount[j])*amount[j]
  if(j === 0 && bal > cid[j][1]){
    changeDue.innerHTML = "Status: INSUFFICIENT_FUNDS";
  } else {
    if(levelBal >= cid[j][1]){
      bal = (bal*100 - cid[j][1]*100)/100;
      changeDue.innerHTML += `<br>${cid[j][0]}: $${cid[j][1]}`;
      cid[j][1] = 0;   
      document.getElementById(`${j}`).textContent = `$${cid[j][1]}`;
      calculate(bal)
    } else {
      bal = ((bal*100 - levelBal*100)/100).toFixed(2);
      console.log(bal)
      changeDue.innerHTML += `<br>${cid[j][0]}: $${levelBal}`;
      cid[j][1] = (cid[j][1]*100 - levelBal*100)/100;
      document.getElementById(`${j}`).textContent = `$${cid[j][1]}`;
      calculate(bal) 
    }
  }
};

for(let l = 0; l < cid.length; l++){
  document.getElementById(`${l}`).innerHTML += `$${cid[l][1]}`;
}

purchaseBtn.addEventListener("click", ()=>{
  const inputValue = parseInt(input.value*100)/100;
  const bal = (inputValue*100 - price*100)/100;
  if(inputValue < price){
    alert("Customer does not have enough money to purchase the item")
  } else if(inputValue === price){
    changeDue.innerHTML = "No change due - customer paid with exact cash"
  } else if(inputValue > price){
    cidTotal();
    display(bal, total);
  }
})



/* 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/131.0.0.0 Safari/537.36

Challenge Information:

Build a Cash Register Project - Build a Cash Register

You shouldn’t have any global variable that need to be changed while the test runs, except above two.

1 Like