Cash Register Syntax issue?

Tell us what’s happening:
Describe your issue in detail here.

For this problem, I have figured out the solution, but there is one aspect that I am stuck on .

When I remove this code:
else if (totalCash < change) {
return {status: “INSUFFICIENT_FUNDS”, change: }
the program functions as it should.

My question is, why does this code not work? Without removing this part of the code, all of the tests pass except the third one. It returns { status: ‘INSUFFICIENT_FUNDS’, change: }. The else if statement is being triggered although the conditions are not being met.

I know that this code is repetitive and not necessary since the last else statement covers all other conditions, but I am still not understanding why the third test is triggering the else if statement? The code should be working fine as it is as far as I can tell?

Thanks in advance!

  **Your code so far**

function checkCashRegister(price, cash, cid) {
const currency = {
  'PENNY': .01,
  'NICKEL': .05,
  'DIME': .1,
  'QUARTER': .25,
  'ONE': 1.00,
  'FIVE': 5.00,
  'TEN': 10.00,
  'TWENTY': 20.00,
  'ONE HUNDRED': 100.00

var change = cash - price
change = change.toFixed(2)

//calc how much money is in the cash drawer
var totalCash = 0;
for (let i=0; i<cid.length; i++) {
  totalCash += cid[i][1]
totalCash = totalCash.toFixed(2)

//if total change due is equal to cash drawer
if (totalCash === change) {
  return {status: "CLOSED", change: cid}
//if total change due is more than cash drawer
else if (totalCash < change) {
  return {status: "INSUFFICIENT_FUNDS", change: []}
//if total change due is less than cash drawer
else {
  var changeArr = []
  for (let i=cid.length-1; i>-1; i--) {
    if (change >= currency[cid[i][0]]) {
      var temp = [cid[i][0], 0]
      while (change >= currency[cid[i][0]] && change > 0 && cid[i][1] > 0) {
        temp[1] += currency[cid[i][0]]
        cid[i][1] -= currency[cid[i][0]]
        change -= currency[cid[i][0]]
        change = change.toFixed(2)
  if (change > 0) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  else {
    return {status: "OPEN", change: changeArr}

console.log(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));
  **Your browser information:**

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

Challenge: Cash Register

Link to the challenge:

I would guess that the problem is that toFixed makes a string.

1 Like

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