Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

Please HELP!! The GitHub ticket on this problem was closed as complited. But well me solution still does not pass last test. Tell meplease so is it wrong? I am stuck on it and cannot get it right. Help to understand please. All outputs with initial data from the last test are correct…

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang='en'>
  <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</title>
  </head>
  <body>
    <main>
      <div class='input-div'>
        <input id='cash' type='text' class='input-cash'/>
      </div>
      <div class = 'buttons'>
        <button id='purchase-btn' class='button' onclick=''>Purchase</button>
      </div>
      <div id='change-due' class='result-msg'>
        
      </div>
    </main>
    <script src="script.js"></script>
  </body>
</html>
/* file: styles.css */

/* 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 cashInput = document.getElementById('cash')
const changeDue = document.getElementById('change-due')
const purchaseBtn = document.getElementById('purchase-btn')

function sumCid(cid) {
  return Number.parseFloat(cid.reduce((sum, el) => {
    return sum = sum + el[1]
  },0).toFixed(2))
}

function giveCash(change, cid) {
  const dict = {
    "PENNY": 0.01,
    "NICKEL": 0.05,
    "DIME": 0.1,
    "QUARTER": 0.25,
    "ONE": 1,
    "FIVE": 5,
    "TEN": 10,
    "TWENTY": 20,
    "ONE HUNDRED": 100
  }
  if (change == 0) {
    return []
  }
  if (sumCid(cid)<change) {
    return [0]
    }
  let nominal = dict[cid[cid.length-1][0]]
  let nominalName = cid[cid.length-1][0]
  let amount = cid[cid.length-1][1]
  let changeIn = 0
  if (change >= nominal && amount > 0) {
    cid = cid.slice(0, cid.length-1)
    do {
      changeIn = Number.parseFloat((changeIn + nominal).toFixed(2))
    } while ((changeIn+nominal) <= change && (changeIn+nominal) <= amount)
    change = Number.parseFloat((change - changeIn).toFixed(2))
    return [...giveCash(change, cid), [nominalName, changeIn]]
  } else if (amount == 0) {
    cid = cid.slice(0, cid.length-1)
    return [...giveCash(change, cid), [nominalName, amount]]
  } else {
    cid = cid.slice(0, cid.length-1)
    return [...giveCash(change, cid)]
  }
}

function checkCashRegister(price, cash, cid) {
  let cashInDrawer = sumCid(cid)
  let change = giveCash((cash-price), cid)
  if (cashInDrawer < (cash - price)) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  if (change[0] === 0) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  if ((cashInDrawer - sumCid(change)) == 0) {
    return {status: "CLOSED", change: change}
  } else {
    return {status: "OPEN", change: change.reverse()}
  }
}

function msgOutput(obj) {
  if (obj.status == 'CLOSED') {
    obj.change = obj.change.reverse().slice(5)
    console.log(obj) 
  } // this is only to fulfil the last test's demand on the output structure
  changeDue.innerText = 'Status: ' + obj.status + ' ' + obj.change.join(' ').replace(/,/g, ': $')
}

purchaseBtn.addEventListener('click', () => {
  if (cashInput.value>price) {
    msgOutput(checkCashRegister(price, cashInput.value, cid));
  } else if (cashInput.value == price) {
    changeDue.innerText = 'No change due - customer paid with exact cash'
  } else {
    alert('Customer does not have enough money to purchase the item')
  }
  cashInput.value = '';
});

Your browser information:

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

Challenge Information:

Build a Cash Register Project - Build a Cash Register

The fix is not in production yet. It needs to be in prod-current and be deployed.

https://github.com/freeCodeCamp/freeCodeCamp/commits/prod-current/

Thank you for support!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.