Exact change - Right returning values but tests won't pass

Exact change - Right returning values but tests won't pass
0

#1

Hey everyone,

Got finally to advanced algorithms !

There’s something i don’t get, i tested each given case and my code is returning the right value but the tests don’t pass.

Here is my code (not very clean, i know):

const coinsAndBills = {
  "ONE HUNDRED": 0,
  TWENTY: 0,
  TEN: 0,
  FIVE: 0,
  ONE: 0,
  QUARTER: 0,
  DIME: 0,
  NICKEL: 0,
  PENNY: 0
}

function checkCashRegister(price, cash, cid) {
  let change = +(cash - price).toFixed(2);
  const changeArr = [];
  const cidArr = cid.reduce(function(acc, cur, i) {
    acc[cur[0]] = [cur[1], cur[1] <= 0 ? false : true];
    return acc;
  }, {});

  while (change >= 100 && cidArr["ONE HUNDRED"][1]) {
    coinsAndBills["ONE HUNDRED"] += 100.00;
    change = Math.round((change - 100) * 100) / 100;
    cidArr["ONE HUNDRED"][0] = Math.round((cidArr["ONE HUNDRED"][0] - 100) * 100) / 100;
    if ((cidArr["ONE HUNDRED"][0] - 100) < 0) cidArr["ONE HUNDRED"][1] = false;
  }
  while (change >= 20 && cidArr["TWENTY"][1]) {
    coinsAndBills["TWENTY"] += 20.00;
    change = Math.round((change - 20) * 100) / 100;
    cidArr["TWENTY"][0] = Math.round((cidArr["TWENTY"][0] - 20) * 100) / 100;
    if ((cidArr["TWENTY"][0] - 20) < 0) cidArr["TWENTY"][1] = false;
  }
  while (change >= 10 && cidArr["TEN"][1]) {
    coinsAndBills["TEN"] += 10.00;
    change = Math.round((change - 10) * 100) / 100;
    cidArr["TEN"][0] = Math.round((cidArr["TEN"][0] - 10) * 100) / 100;
    if ((cidArr["TEN"][0] - 10) < 0) cidArr["TEN"][1] = false;
  }
  while (change >= 5 && cidArr["FIVE"][1]) {
    coinsAndBills["FIVE"] += 5.00;
    change = Math.round((change - 5) * 100) / 100;
    cidArr["FIVE"][0] = Math.round((cidArr["FIVE"][0] - 5) * 100) / 100;
    if ((cidArr["FIVE"][0] - 5) < 0) cidArr["FIVE"][1] = false;
  }
  while (change >= 1 && cidArr["ONE"][1]) {
    coinsAndBills["ONE"] += 1.00;
    change = Math.round((change - 1.00) * 100) / 100;
    cidArr["ONE"][0] = Math.round((cidArr["ONE"][0] - 1) * 100) / 100;
    if ((cidArr["ONE"][0] - 1.00) < 0) cidArr["ONE"][1] = false;
  }
  while (change >= 0.25 && cidArr["QUARTER"][1]) {
    coinsAndBills["QUARTER"] += 0.25;
    change = Math.round((change - 0.25) * 100) / 100;
    cidArr["QUARTER"][0] = Math.round((cidArr["QUARTER"][0] - 0.25) * 100) / 100;
    if ((cidArr["QUARTER"][0] - 0.25) < 0) cidArr["QUARTER"][1] = false;
  }
  while (change >= 0.10 && cidArr["DIME"][1]) {
    coinsAndBills["DIME"] += 0.10;
    change = Math.round((change - 0.10) * 100) / 100;
    cidArr["DIME"][0] = Math.round((cidArr["DIME"][0] - 0.10) * 100) / 100;
    if ((cidArr["DIME"][0] - 0.10) < 0) cidArr["DIME"][1] = false;
  }
  while (change >= 0.05 && cidArr["NICKEL"][1]) {
    coinsAndBills["NICKEL"] += 0.05;
    change = Math.round((change - 0.05) * 100) / 100;
    cidArr["NICKEL"][0] = Math.round((cidArr["NICKEL"][0] - 0.05) * 100) / 100;
    if ((cidArr["NICKEL"][0] - 0.05) < 0) cidArr["NICKEL"][1] = false;
  }
  while (change >= 0.01 && cidArr["PENNY"][1]) {
    coinsAndBills["PENNY"] += 0.01;
    change = Math.round((change - 0.01) * 100) / 100;
    cidArr["PENNY"][0] = Math.round((cidArr["PENNY"][0] - 0.01) * 100) / 100;
    if ((cidArr["PENNY"][0] - 0.01) < 0) cidArr["PENNY"][1] = false;
  }
  
  const newArr = Object.entries(cidArr).map(el => {
    return [el[0], [ Math.round(el[1][0] * 100) / 100, el[1][1]]];
  });
  
  if (newArr.every(el => el[1][1] === false)) return "Closed";
  if (change > 0) return "Insufficient Funds";
 
  // Here is your change, ma'am.
  return Object.entries(coinsAndBills).map(el => [el[0], el[1]]).filter(el => el[1] !== 0);
}

Please help !


#2

Place const coinsAndBills inside the function and you’ll be left with one failing test.


#3

Thank you ! Just fixed the last failing test.

But i think i will revisit it soon as i’m not satisfied with my code, want to do it with .reduce().

Thank you again anyway.