Cash register guidance

Hi,

I’m just doing some testing to see what does and doesn’t work, and for some reason I can’t get this code to pass the last test:

function checkCashRegister(price, cash, cid) {
  let till = cid.reverse();
  let tillTotal = parseFloat(till.reduce((preVal, curVal) => preVal + curVal[1], 0).toFixed(2));
  let change = cash - price;

  if (tillTotal === change) {
    return {status: "CLOSED", change: cid};
  }
}

If I paste in the ‘cid’ array directly it passes but it doesn’t if I insert as a variable, is there something I’m missing here?

Thanks.

does reverse change the array on which it is used?

1 Like

Yes it does. It changes the array in place and returns (a reference to) the reversed array.

(I don’t know if you were sincerely asking, or trying to give bayzoo a hint. Sorry if it is the latter :flushed:)

1 Like

Thanks both for saving my sanity - last time I try to code and look after a 1 yr old at the same time… amended code below:

function checkCashRegister(price, cash, cid) {
  let till = [...cid].reverse();
  let tillTotal = parseFloat(till.reduce((preVal, curVal) => preVal + curVal[1], 0).toFixed(2));
  let change = cash - price;

  if (tillTotal === change) {
    return {status: "CLOSED", change: cid};
  }
}

Hello @bayzoo,

Glad we could help solve your problem!

Another way you could go about this would be to declare till as a copy of cid then reverse till.

let till = cid
till.reverse()
1 Like

Excellent, thanks! It’s good to see different ways of achieving the same result. I achieved my JS certification last night, so I’m pretty happy now I can get started on some projects.

Awesome to hear! Good luck on the projects. In my opinion, the projects are the most fun part of learning to code.

1 Like