A Little Help on "Cash Register"

Hi fellow campers. I have an issue whereby all the other tests pass but only one, which is very similar to another one that did pass. I was wondering if anyone could point out some hint or something as to where I messed up, because to me it seems logical and should work (and it does for most of the tests!). It is only failing the third test.

Here is my code (it is quite long, so sorry if this is an inconvenience):

let array = JSON.parse(JSON.stringify(cid));
  let change = cash-price;
  let pennyItem = array.find(item => item[0] === "PENNY");
  let nickelItem = array.find(item => item[0] === "NICKEL");
  let dimeItem = array.find(item => item[0] === "DIME"); 
  let quarterItem = array.find(item => item[0] === "QUARTER");
  let oneItem = array.find(item=> item[0] === "ONE");
  let fiveItem = array.find(item=> item[0] === "FIVE");
  let tenItem = array.find(item=> item[0] === "TEN");
  let twentyItem = array.find(item=> item[0] === "TWENTY");
  let oneHundredItem = array.find(item=> item[0] === "ONE HUNDRED");
  let registerValue = pennyItem[1] + nickelItem[1] + dimeItem[1] + quarterItem[1] + oneItem[1] + fiveItem[1] + tenItem[1] + twentyItem[1] + oneHundredItem[1];
 if (change==registerValue) {
    return {status: "CLOSED", change: cid}
  };
  while (change>0 && change<registerValue) {
if (change>=100 && oneHundredItem[1]>=100) {
  change -= 100;
  oneHundredItem[1] -= 100;
} else if (change>=20 && twentyItem[1]>=20) {
  change -= 20;
  twentyItem[1] -= 20;
} else if (change>=10 && tenItem[1]>=10) {
  change -= 10;
  tenItem[1] -= 10;
} else if (change>=5 && fiveItem[1]>=5) {
  change -= 5;
  fiveItem[1] -= 5;
} else if (change>=1 && oneItem[1]>=1) {
  change -= 1;
  oneItem[1] -= 1;
} else if (change>=0.25 && quarterItem[1]>=0.25) {
  change -= 0.25;
  quarterItem[1] -= 0.25;
} else if (change>=0.1 && dimeItem[1]>=0.1) {
  change -= 0.1;
  dimeItem[1] -= 0.1;
} else if (change>=0.05 && nickelItem[1]>=0.05) {
  change -= 0.05;
  nickelItem[1] -= 0.05;
} else if (change>=0.01 && pennyItem[1]>=0.01) {
  change -= 0.01;
  pennyItem[1] -= 0.01;
} else {
  return {status: "INSUFFICIENT_FUNDS", change: []}
}
  };
  let response = [];
  if (oneHundredItem[1]!==cid.find(item=>item[0]==="ONE HUNDRED")[1]) {
    response.push(["ONE HUNDRED", cid.find(item=>item[0]==="ONE HUNDRED")[1]-oneHundredItem[1]])
  };
  if (twentyItem[1]!==cid.find(item=>item[0]=="TWENTY")[1]) {
    response.push(["TWENTY", cid.find(item=>item[0]==="TWENTY")[1]-twentyItem[1]])
  };
  if (tenItem[1]!==cid.find(item=>item[0]==="TEN")[1]) {
    response.push(["TEN", cid.find(item=>item[0]==="TEN")[1]-tenItem[1]])
  };
  if (oneItem[1]!==cid.find(item=>item[0]==="ONE")[1]) {
    response.push(["ONE", cid.find(item=>item[0]==="ONE")[1]-oneItem])
  };
  if (quarterItem[1]!==cid.find(item=>item[0]==="QUARTER")[1]) {
    response.push(["QUARTER", cid.find(item=>item[0]==="QUARTER")[1]-quarterItem[1]])
  };
  if (dimeItem[1]!==cid.find(item=>item[0]==="DIME")[1]) {
    response.push(["DIME", cid.find(item=>item[0]==="DIME")[1]-dimeItem[1]])
  };
  if (nickelItem[1]!==cid.find(item=>item[0]==="NICKEL")[1]) {
response.push(["NICKEL", cid.find(item=>item[0]==="NICKEL")[1]-nickelItem[1]])
  };
  if (pennyItem[1]!==cid.find(item=>item[0]==="PENNY")[1]) {
    response.push(["PENNY", cid.find(item=>item[0]==="PENNY")[1]-pennyItem[1]])
  };
  if (change!==0) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  };
  return {status: "OPEN", change: response}
} 

Can you tell us which test fails? That would help us troubleshoot.

Also, it would be helpful if you include the full function definition so we can run your code more easily.

Thanks

1 Like

The third test that says,
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]]}

It’s the project that says “Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument…”

It gives the function “function checkCashRegister(price, cash, cid) {…”

This is the starting function definition:

function checkCashRegister(price, cash, cid) {
  let change;
  return change;
}

It would be helpful if you provided your full function definition.

I’m including the link to the project so it is easier for people to be able to help you:

1 Like

So I have cleaned up some of the typos/little mistakes in the code and the issue still remains. I’m not sure why, but it seems like every denomination below a quarter (dimes, nickels, and pennies) just does not work for some reason. Does anyone know why that might be? Here’s my cleaned up code.

  let array = JSON.parse(JSON.stringify(cid));
  let change = cash-price;
  let pennyItem = array.find(item=> item[0] === "PENNY");
  let nickelItem = array.find(item=> item[0] === "NICKEL");
  let dimeItem = array.find(item=> item[0] === "DIME"); 
  let quarterItem = array.find(item=> item[0] === "QUARTER");
  let oneItem = array.find(item=> item[0] === "ONE");
  let fiveItem = array.find(item=> item[0] === "FIVE");
  let tenItem = array.find(item=> item[0] === "TEN");
  let twentyItem = array.find(item=> item[0] === "TWENTY");
  let oneHundredItem = array.find(item=> item[0] === "ONE HUNDRED");
  let registerValue = pennyItem[1] + nickelItem[1] + dimeItem[1] + quarterItem[1] + oneItem[1] + fiveItem[1] + tenItem[1] + twentyItem[1] + oneHundredItem[1];
 if (change==registerValue) {
    return {status: "CLOSED", change: cid}
  };
  while (change>0 && change<registerValue) {
if (change>=100 && oneHundredItem[1]>=100) {
  change -= 100;
  oneHundredItem[1] -= 100;
} else if (change>=20 && twentyItem[1]>=20) {
  change -= 20;
  twentyItem[1] -= 20;
} else if (change>=10 && tenItem[1]>=10) {
  change -= 10;
  tenItem[1] -= 10;
} else if (change>=5 && fiveItem[1]>=5) {
  change -= 5;
  fiveItem[1] -= 5;
} else if (change>=1 && oneItem[1]>=1) {
  change -= 1;
  oneItem[1] -= 1;
} else if (change>=0.25 && quarterItem[1]>=0.25) {
  change -= 0.25;
  quarterItem[1] -= 0.25;
} else if (change>=.1 && dimeItem[1]>=.1) {
  change -= .1;
  dimeItem[1] -= .1;
} else if (change>=.05 && nickelItem[1]>=.05) {
  change -= .05;
  nickelItem[1] -= .05;
} else if (change>=.01 && pennyItem[1]>=.01) {
  change -= .01;
  pennyItem[1] -= .01;
} else {
  return {status: "INSUFFICIENT_FUNDS", change: []}
}
  };
  let response = [];
  if (oneHundredItem[1]!==cid.find(item=>item[0]==="ONE HUNDRED")[1]) {
    response.push(["ONE HUNDRED", cid.find(item=>item[0]==="ONE HUNDRED")[1]-oneHundredItem[1]])
  };
  if (twentyItem[1]!==cid.find(item=>item[0]=="TWENTY")[1]) {
    response.push(["TWENTY", cid.find(item=>item[0]==="TWENTY")[1]-twentyItem[1]])
  };
  if (tenItem[1]!==cid.find(item=>item[0]==="TEN")[1]) {
    response.push(["TEN", cid.find(item=>item[0]==="TEN")[1]-tenItem[1]])
  };
  if (fiveItem[1]!==cid.find(item=>item[0]==="FIVE")[1]) {
    response.push(["FIVE", cid.find(item=> item[0]==="FIVE")[1]-fiveItem[1]])
  };
  if (oneItem[1]!==cid.find(item=>item[0]==="ONE")[1]) {
    response.push(["ONE", cid.find(item=>item[0]==="ONE")[1]-oneItem[1]])
  };
  if (quarterItem[1]!==cid.find(item=>item[0]==="QUARTER")[1]) {
    response.push(["QUARTER", cid.find(item=>item[0]==="QUARTER")[1]-quarterItem[1]])
  };
  if (dimeItem[1]!==cid.find(item=>item[0]==="DIME")[1]) {
    response.push(["DIME", cid.find(item=>item[0]==="DIME")[1]-dimeItem[1]])
  };
  if (nickelItem[1]!==cid.find(item=>item[0]==="NICKEL")[1]) {
response.push(["NICKEL", cid.find(item=>item[0]==="NICKEL")[1]-nickelItem[1]])
  };
  if (pennyItem[1]!==cid.find(item=>item[0]==="PENNY")[1]) {
    response.push(["PENNY", cid.find(item=>item[0]==="PENNY")[1]-pennyItem[1]])
  };
  if (change!==0) {
    return {status: "INSUFFICIENT_FUNDS", change: []}
  } else {
  return {status: "OPEN", change: response}}
}

OK I have solved the issue. I think maybe they should give a warning about working with decimals in JavaScript on this project…caused me much consternation.

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