JavaScript Cash Register Certification Project not passing 1 test

This solution I came up with is not passing one test…been on it for a while.
I would like to know what the issue is with the code and how it can be fixed. Thank you.
Please forgive the disastrous design and variable naming.

function checkCashRegister(price, cash, cid) {
    var change;
    let cidArr = [];
    let monObj = {};
    change = cash - price;
    let a = cid.filter(e => e[1] > change).every(e =>{ e[1] < change});
    
    
    for (const money of cid) {

        if (change == money[1]) {
            
            monObj.status = "CLOSED";
            monObj.change = cid;
            //console.log(monObj);
            break;
        } else if (a == true) {
         
            monObj.status = "INSUFFICIENT_FUNDS";
            monObj.change = [];
           
            break;
        } else {

            //console.log(change, money[1]);
            monObj.status = "OPEN";
            if (change >= 20 && change < 100) {
                let tc = 0;
                while (change >= 20 && change < 100) {
                    change -= 20;
                    tc += 20;
                    if (tc == 60) {
                        break
                    }
                } cidArr.push(["TWENTY", tc]); 
            }
            if (change >= 10 && change < 20 || change > 10) {
                let tc = 0;
                while (change >= 10 && change < 20 || change > 10) {
                    change -= 10;
                    tc += 10;
                    if (tc == 20) {
                        break
                    }
                } cidArr.push(["TEN", tc]);
            }
            if (change >= 5 && change < 10 || change > 5) {
                let tc = 0;
                while (change >= 5 && change < 10 || change > 5) {
                    change -= 5;
                    tc += 5;
                    if (tc == 55) {
                        break
                    }
                } cidArr.push(["FIVE", tc]); 
            }
            if (change >= 1 && change < 5 || change > 1) {
                let tc = 0;
                while (change >= 1 && change < 5 || change > 1) {
                    change -= 1;
                    tc += 1;
                    if (tc == 90) {
                        break
                    }
                } cidArr.push(["ONE", tc]);
            }

            if (change >= 0.25 && change < 1 || change > 0.25) {
                let tc = 0

                while (change >= 0.25 && change < 1 || change > 0.25) {
                    change -= 0.25;
                    tc += 0.25;
                    if (tc == 4.25) {
                        break
                    }
                } cidArr.push(["QUARTER", tc]);
            }
            if (change >= 0.1 && change < 0.25 || change > 0.1) {
                let tc = 0;
                while (change >= 0.1 && change < 0.25 || change > 0.1) {
                    change -= 0.1;
                    tc += 0.1;
                    if (tc == 3.1) {
                        break
                    }
                } cidArr.push(["DIME", tc]);
            }
            if (change >= 0.05 && change < 0.1 || change > 0.05) {
                let tc = 0;
                while (change >= 0.05 && change < 0.1 || change > 0.05) {
                    change -= 0.05;
                    tc += 0.05;
                    if (tc == 2.05) {
                        break
                    }
                } cidArr.push(["NICKEL", tc]); 
            }
            if (change >= 0.01 && change < 0.05 || change > 0.01) {
                let tc = 0;
                while (change >= 0.01 && change < 0.05 || change > 0.01) {
                    change = Math.round(change * 1000) / 1000;
                    change -= 0.01;
                    tc += 0.01;
                    if (tc == 1.01) {
                        break
                    }
                } cidArr.push(["PENNY", tc]); 
            }


        }
    

        monObj.change = cidArr;
    }

    return monObj;
}


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

Hello!

You’re subtracting money when there’s none.

The test that fails uses these values:

19.5, 20, [
      ["PENNY", 0.01],
      ["NICKEL", 0],
      ["DIME", 0],
      ["QUARTER", 0],
      ["ONE", 1],
      ["FIVE", 0],
      ["TEN", 0],
      ["TWENTY", 0],
      ["ONE HUNDRED", 0],
]

You’re expected to give 0.5 of change, for which you need 50 pennies, 10 nickels, 5 dimes, 2 quarters or a combination of these.

The problem you’re having is here:

if ((change >= 0.25 && change < 1) || change > 0.25) {
    console.log("* Quarter:", change);
    let tc = 0;

    while ((change >= 0.25 && change < 1) || change > 0.25) {
        change -= 0.25;
        tc += 0.25;
        if (tc == 4.25) {
            break;
        }
    }
    cidArr.push(["QUARTER", tc]);
    console.log("cidArr:", cidArr);
}

Where your code is not checking that there’s enough money (quarters in this case) to return but still subtracts from the change variable, which is actually causing the register to close instead of returning insufficient funds.

Does it help? Did I make myself clear?

This is your own code but with more console.logs to help you see what happens:

function checkCashRegister(price, cash, cid) {
  var change;
  let cidArr = [];
  let monObj = {};
  change = cash - price;
  let a = cid
    .filter((e) => e[1] > change)
    .every((e) => {
      e[1] < change;
    });
  console.log("Initial change:", change);

  for (const money of cid) {
    console.log("--- Money:", money);
    console.log("$ Current change:", change);
    if (change == money[1]) {
      console.log("Closing");
      monObj.status = "CLOSED";
      monObj.change = cid;
      break;
    } else if (a == true) {
      console.log("Not enough");
      monObj.status = "INSUFFICIENT_FUNDS";
      monObj.change = [];
      break;
    } else {
      console.log("Subtracting money");
      monObj.status = "OPEN";
      if (change >= 20 && change < 100) {
        let tc = 0;
        while (change >= 20 && change < 100) {
          change -= 20;
          tc += 20;
          if (tc == 60) {
            break;
          }
        }
        cidArr.push(["TWENTY", tc]);
      }
      if ((change >= 10 && change < 20) || change > 10) {
        let tc = 0;
        while ((change >= 10 && change < 20) || change > 10) {
          change -= 10;
          tc += 10;
          if (tc == 20) {
            break;
          }
        }
        cidArr.push(["TEN", tc]);
      }
      if ((change >= 5 && change < 10) || change > 5) {
        let tc = 0;
        while ((change >= 5 && change < 10) || change > 5) {
          change -= 5;
          tc += 5;
          if (tc == 55) {
            break;
          }
        }
        cidArr.push(["FIVE", tc]);
      }
      if ((change >= 1 && change < 5) || change > 1) {
        let tc = 0;
        while ((change >= 1 && change < 5) || change > 1) {
          change -= 1;
          tc += 1;
          if (tc == 90) {
            break;
          }
        }
        cidArr.push(["ONE", tc]);
      }

      if ((change >= 0.25 && change < 1) || change > 0.25) {
        console.log("* Quarter:", change);
        let tc = 0;

        while ((change >= 0.25 && change < 1) || change > 0.25) {
          change -= 0.25;
          tc += 0.25;
          if (tc == 4.25) {
            break;
          }
        }
        cidArr.push(["QUARTER", tc]);
        console.log("cidArr:", cidArr);
      }
      if ((change >= 0.1 && change < 0.25) || change > 0.1) {
        console.log("* Dime:", change);
        let tc = 0;
        while ((change >= 0.1 && change < 0.25) || change > 0.1) {
          change -= 0.1;
          tc += 0.1;
          if (tc == 3.1) {
            break;
          }
        }
        cidArr.push(["DIME", tc]);
        console.log("cidArr:", cidArr);
      }
      if ((change >= 0.05 && change < 0.1) || change > 0.05) {
        console.log("* Nickel:", change);
        let tc = 0;
        while ((change >= 0.05 && change < 0.1) || change > 0.05) {
          change -= 0.05;
          tc += 0.05;
          if (tc == 2.05) {
            break;
          }
        }
        cidArr.push(["NICKEL", tc]);
        console.log("cidArr:", cidArr);
      }
      if ((change >= 0.01 && change < 0.05) || change > 0.01) {
        console.log("* Penny:", change);
        let tc = 0;
        while ((change >= 0.01 && change < 0.05) || change > 0.01) {
          change = Math.round(change * 1000) / 1000;
          change -= 0.01;
          tc += 0.01;
          if (tc == 1.01) {
            break;
          }
        }
        cidArr.push(["PENNY", tc]);
        console.log("cidArr:", cidArr);
      }
    }

    monObj.change = cidArr;
  }

  return monObj;
}
2 Likes

Yes it helped…I was only tracking the change and failed to track the cid as deductions were made.

I really appreciate the help…I mean my code was basically a wilderness of redundancies…lol. :sweat_smile:

1 Like

After much brainstorming and on the verge of giving up (I think I jumped of the verge actually) My code was revamped by a certain awesome mind.
Here is the working solution…I really loved the fact that it kind of maintained the logic/concept I had started on and is very elaborate.
Alas, I AM FREE! :joy: :sweat_smile:


function checkCashRegister(price, cash, cid) {
    var change;  
    let cidArr = [];
    let monObj = {};
    change = cash - price;

var cidCopy = [];
//copy all cid money to cidCopy array
cid.forEach(e=> cidCopy.push(e[1]));
//add all money in array
let cidSum = cidCopy.reduce((sum,e)=> e +sum)

if(change == cidSum) {
  monObj.status = 'CLOSED'
  monObj.change = cid
  console.log(monObj);
  return monObj;
} else{
    // check if you have ONE HUNDRED notes 
    if (cid[8][1] > 0) {
        var deduction
      // check if ONE HUNDRED notes can be used to pay for the change
      if (Math.floor(change / 100) > 0) {
        // calculate how much can be reduced from the change using ONE HUNDRED bills
        deduction = Math.floor(change / 100) * 100
        // check if we have the required number of bills, if not, take whatever we have
        if (deduction > cid[8][1]) {
            deduction = cid[8][1]
        }
        // reduce the change by the deduction and round to 2 decimal places
        change = Math.round((change-deduction) * 100) / 100;
        // reduce the cash register
        cid[8][1] -= deduction
        cidArr.push(["ONE HUNDRED", deduction]);
      }
    }
    // check if you have TWENTY notes 
    if (cid[7][1] > 0) {
      var deduction
      // check if TWENTY notes can be used to pay for the change
      if (Math.floor(change / 20) > 0) {
        deduction = Math.floor(change / 20) * 20
        if (deduction > cid[7][1]) {
            deduction = cid[7][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[7][1] -= deduction
        cidArr.push(["TWENTY", deduction]);
      }
    }
    // check if you have TEN notes 
    if (cid[6][1] > 0) {
      var deduction
      // check if TEN notes can be used to pay for the change
      if (Math.floor(change / 10) > 0) {
        deduction = Math.floor(change / 10) * 10
        if (deduction > cid[6][1]) {
            deduction = cid[6][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[6][1] -= deduction
        cidArr.push(["TEN", deduction]);
      }
    }
    // check if you have FIVE notes 
    if (cid[5][1] > 0) {
      var deduction
      // check if FIVE notes can be used to pay for the change
      if (Math.floor(change / 5) > 0) {
        deduction = Math.floor(change / 5) * 5
        if (deduction > cid[5][1]) {
            deduction = cid[5][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[5][1] -= deduction
        cidArr.push(["FIVE", deduction]);
      }
    }
    // check if you have ONE notes 
    if (cid[4][1] > 0) {
      var deduction
      // check if ONE notes can be used to pay for the change
      if (Math.floor(change / 1) > 0) {
        deduction = Math.floor(change / 1) * 1
        if (deduction > cid[4][1]) {
            deduction = cid[4][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[4][1] -= deduction
        cidArr.push(["ONE", deduction]);
      }
    }
    // check if you have QUARTER notes 
    if (cid[3][1] > 0) {
      var deduction
      // check if QUARTER notes can be used to pay for the change
      if (Math.floor(change / 0.25) > 0) {
        deduction = Math.floor(change / 0.25) * 0.25
        if (deduction > cid[3][1]) {
            deduction = cid[3][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[3][1] -= deduction
        cidArr.push(["QUARTER", deduction]);
      }
    }
    // check if you have DIME notes 
    if (cid[2][1] > 0) {
      var deduction
      // check if DIME notes can be used to pay for the change
      if (Math.floor(change / 0.1) > 0) {
        deduction = Math.floor(change / 0.1) * 0.1
        if (deduction > cid[2][1]) {
            deduction = cid[2][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[2][1] -= deduction
        cidArr.push(["DIME", deduction]);
      }
    }
        // check if you have NICKEL notes 
    if (cid[1][1] > 0) {
      var deduction
      // check if NICKEL notes can be used to pay for the change
      if (Math.floor(change / 0.05) > 0) {
        deduction = Math.floor(change / 0.05) * 0.05
        if (deduction > cid[1][1]) {
            deduction = cid[1][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[1][1] -= deduction
        cidArr.push(["NICKEL", deduction]);
      }
    }
        // check if you have PENNY notes 
    if (cid[0][1] > 0) {
      var deduction
      // check if PENNY notes can be used to pay for the change
      if (Math.floor(change / 0.01) > 0) {
        deduction = Math.floor(change / 0.01) * 0.01
        if (deduction > cid[0][1]) {
            deduction = cid[0][1]
        }
        change = Math.round((change-deduction) * 100) / 100;
         cid[0][1] -= deduction
        cidArr.push(["PENNY", deduction]);
      }
    }

  }  change = Math.round(change * 100) / 100;
    if (change == 0) {
      monObj.change = cidArr;
      for (var i = 0; i< cid.length; i++) {
        if (cid[i][1] != 0) {
          monObj.status = "OPEN";
          return monObj;
        }
      }
      monObj.status = "CLOSED";
    } else {
      console.log(change)
      monObj.status = "INSUFFICIENT_FUNDS";
      monObj.change = [];
    }

    return monObj;
}

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