Cash Register - Been Stuck for a few Hours

Tell us what’s happening:
So if I uncomment the last if statement I’ll get an error about it being a penny off, at least when testing this with my local setup. Been playing with this for a few hours now and just can’t seem to find what is not working with that last test.

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]])should return {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}

Instead of 0.04 for pennies, I get 0.03

Your code so far


function totalInDrawer(acc, [currency, total]) {
  return acc += total
}

function round(value, decimals) {
  return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}

const dem = {
  'ONE HUNDRED': 100,
  'TWENTY': 20,
  'TEN': 10,
  'FIVE': 5,
  'ONE': 1,
  'QUARTER': 0.25,
  'DIME': 0.10,
  'NICKLE': 0.05,
  'PENNY': 0.01
}

function checkCashRegister(price, paid, cid) {
  const cashTotal = round(cid.reduce(totalInDrawer, 0), 2)
  let changeToGive = paid - price

  if (cashTotal < changeToGive) {
    return {
      status: 'INSUFFICIENT_FUNDS',
      change: []
    }
  } else if (cashTotal === changeToGive) {
    return {
      status: 'CLOSED',
      change: cid
    }
  }

  const change = cid.reverse().reduce((acc, [currency, amount]) => {
    const needed = parseInt(changeToGive / dem[currency])
    const onHand = parseInt(amount / dem[currency])

    if (needed > 0) {
      if (needed > onHand) {
        changeToGive -= amount
        return [ ...acc, [currency, amount]]
      }

      const total = round(needed * dem[currency], 2)

      changeToGive -= total

      return [...acc, [currency, total]]
    }

    return acc
  }, [])


  let totalChange = change.reduce(totalInDrawer, 0)

  if (totalChange < paid - price) {
    return {
      status: 'INSUFFICIENT_FUNDS',
      change: []
    }
  }

  return {
    status: 'OPEN',
    change
  }
}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

  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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36.

Link to the challenge: