My Cash Register gives the correct output , but won't pass

Tell us what’s happening:

My code seems to work and gets the appropriate results, but it does not pass the tests. I’ll take any help given. Thanks, Y’all are awesome.

Your code so far


function checkCashRegister(price, cash, cid) {
  let money = {'PENNY': 0.01, 'NICKEL': 0.05, 'DIME': 0.1, 'QUARTER': 0.25, 'ONE': 1, 'FIVE': 5, 'TEN': 10, 'TWENTY': 20, 'FIFTY': 50, 'ONE HUNDRED': 100}
  let total = cid.map((x ) => x[1]).reduce((a, b) => a+b).toFixed(2); 
  let quantity = cid.map((x) => Math.ceil(x[1] / money[x[0]]));
  let chan = cash - price
  let change = chan.toFixed(2);
  let changes = chan.toFixed(2);
  let length = quantity.length -1;
  let changeArr = [];
  let status = "";
  function changer(change, len) {
    if (change > 0 && len < 0) {
      status = "INSUFFICIENT_FUNDS"
      changeArr = []
    } else if (len < 0 && total == changes){
      status = "CLOSED"
    } else if (len < 0){
      console.log('paid', total)
      status = "OPEN"
    } else {
      let bills = change / money[cid[len][0]]
      let draw = quantity[len]
      if (bills >= 1  && draw >=1) {
        if (draw > bills) {
        let given = money[cid[len][0]] * Math.floor(bills)
        let changeLeft = change -= given;
        console.log([cid[len][0], given.toFixed(2), changeLeft.toFixed(2), money[cid[len][0]], Math.round(bills) ])
        changeArr.push([cid[len][0], given.toFixed(2)])                
        len--
        changer(changeLeft.toFixed(2), len)
        } else {
        let given = money[cid[len][0]] * draw
        let changeLeft = change -= given;
        console.log([cid[len][0], given.toFixed(2), changeLeft.toFixed(2)])
        changeArr.push([cid[len][0], given.toFixed(2)])
        change -= given;
        len--
        changer(changeLeft.toFixed(2), len)
        }
      } else {
        len--
        changer(change, len)
      }
    }
  }
  changer(change, length);
  return {status: status, change: changeArr}
}

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

you just need to compare your answer to the expected one to understand what needs to change.

for eg. here is one of the scenarios that fail
checkCashRegister(19.5, 20, [[“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: [[“QUARTER”, 0.5]]}

and when I run that same test using your function I get:
{ status: ‘OPEN’, change: [ [ ‘QUARTER’, ‘0.50’ ] ] }

Do you see the difference?

The expected one is:
{status: “OPEN”, change: [[“QUARTER”, 0.5]]}
Yours is:
{ status: ‘OPEN’, change: [ [ ‘QUARTER’, ‘0.50’ ] ] }

So it appears you are returning change as a string rather than a number (because I see quotes around your 0.50 value)

1 Like

I actually noticed the problem just a few minutes after posting. I didn’t realize toFixed put out a string . All fixed, cleaned up, submitted, and certification received.

You’re awesome, thanks so much,
Ed

1 Like