My Cash Register

Hey! I’m doing the last exercise from Javascript Algorithms and Data Structure Projects.
Probably I should take a walk, rest and find out whats happening with my code but I’m kinda anxious to end this code so I’m asking for help


function checkCashRegister(price, cash, cid) {
  function roundHalf(num) {
    return Math.round(num*2)/2;

  const map = {
    "ONE HUNDRED": 100,
    "TWENTY": 20,
    "TEN": 10,
    "FIVE": 5,
    "ONE": 1,
    "QUARTER": 0.25,
    "DIME": 0.1,
    "NICKEL": 0.05,
    "PENNY": 0.01
  let totalMoney=0;
  let change = cash - price;

  for(let index = 0;index<[...cid].length;index++){
    totalMoney+= cid[index][1];

  if (totalMoney < change){
    return {status: "INSUFFICIENT_FUNDS", change: []};

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

  let changeArr = [];
  let value,coinNumber=0,coinTotal;
  for(let index = 0; index < Object.keys(map).length;index++){
    if(change > Object.values(map)[index]){
      if(cid[Object.values(map).length - index -1][1] != 0){
        coinTotal = cid[Object.keys(map).length -1-index][1]/Object.values(map)[index];
        while(change >= Object.values(map)[index] && coinTotal > 0){
          change -= Object.values(map)[index];
        value = coinNumber * Object.values(map)[index];
    return {status: "INSUFFICIENT_FUNDS", change: []};

  const obj = {
    status: "OPEN",
    change: changeArr,
  return obj;

console.log(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]]));

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Welcome, QIRoss.

This is the classic case of do not work with floats, when dealing with money, because you will end up with rounding errors.

Quick fix: Use Number.toFixed(2) to round the numbers to 2 decimal places (all numbers).

Better fix: change all numbers into integers, and convert back, at the end.

Let us know, if you get stuck.

PS: Now is an excellent time to put to use the lessons taught about shallow copying…

1 Like