Build a Cash Register Project - Build a Cash Register

Tell us what’s happening:

This code returns everything correct, I have even screwed in a crutch to fulfill this last condition to return why the God knows why the change-due-string beginning with QUARTER, though this the same task in the practice corse were solved by this same code and all worked fine. But now having this strange output on the CLOSED status case, I still can not pass last test. Please help me, what am I doing wrong?

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

I didn’t test your code or look at it but there seem to be some bugs with the challenge.

1 Like

Does It mean we should humbly wait for it to be fixed?
Or to take some-s against a sea of troubles? :face_with_monocle:

If the bug is causing correct code to fail you do not have much choice but to wait for the fix.

1 Like

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