Cash Register proj feedback

So i finished the final project of the JS Algorithms and Data Structures course,
and iā€™d love some feedback :upside_down_face:
How did i do? What do you dislike about it? What do you like about it? Is there a cleaner way of doing stuff like changeObj[billObj[billArr[i]]] += billArr[i]; or do i have to brace myself and deal with it? :grin:

I appreciate all the feedback.
Thank you.

  **Your code so far**

function checkCashRegister(price, cash, cid) {
//coins and bills
const billArr = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
//coins and bills number:string pairs
const billObj = {
  100: 'ONE HUNDRED',
  20: 'TWENTY',
  10: 'TEN',
  5: 'FIVE',
  1: 'ONE',
  0.25: 'QUARTER',
  0.1: 'DIME',
  0.05: 'NICKEL',
  0.01: 'PENNY'
//makes a copy of cid and turns it into an object
const cidCopy = arrToObj(cid);
//the total amount of money in the cash drawer
//sometimes it may be influenced by the power of garbage,
//turning it into smth like 36.4699999999999999.
//Gotta be careful with that.
const cidTotal = cid.reduce((acc, item) => {
  return acc += item[1]
}, 0);
//the total change to be returned
let changeNum = cash - price;
//all the change is going to live here
const changeObj = {};
//used to iterate over the billArr array,
//from the most significant bill, to the least significant coin
let i = 0;

//if the total amount of cash in drawer is equal
//to the change due, return cid and status, exit function
//had to resort to some functions to turn cidTotal
//into a 2 dec places float, as it should have been
if(parseFloat(cidTotal.toFixed(2)) === changeNum){
  const res = {
    status: 'CLOSED',
    change: cid
  return res;
//if the function reached this point,
//then cidTotal !== changeNum
//proceed building changeObj
while(changeNum > 0){
//if i moved past the penny(0.01 i.e last item in the array),
//then we don't have enough money or
//we can't return the exact change with the money we have 
  if(i >= billArr.length){
    const res = {status: "INSUFFICIENT_FUNDS", change: []}
    return res;
 //if the current bill/coin makes the change bigger than
//it should be or there is zero of the current bill/coin
// in the drawer, try the next bill/coin
  if(changeNum - billArr[i] < 0 || cidCopy[billObj[billArr[i]]] === 0){
  } else {
  //power of garbage influence spotted here
  //subtract the current bill/coin from the change due
    changeNum = parseFloat((changeNum - billArr[i]).toFixed(2));
  //subtract the current bill/coin from the cid
    cidCopy[billObj[billArr[i]]] -= billArr[i];
 //if changeObj already has the properties "TWENTY",
 //"TEN", "DIME" etc., add the current bill/coin to its 
//corresponding prop in the obj
      changeObj[billObj[billArr[i]]] += billArr[i];
    //if not, create that prop and set it
   //to the current bill/coin value
    else {
      changeObj[billObj[billArr[i]]] = billArr[i];
//if the function reached this point, then indeed we can return
//the exact change, and there would still be
//money left in the drawer
const res = {
  status: 'OPEN',
  //turn the object changeObj into an array
  change: Object.entries(changeObj)
return res;

//turns very specific looking arrays into objects
function arrToObj(arr){
const obj = {};
arr.forEach(item => {
  obj[item[0]] = item[1];
return obj;

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05],
 ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55],
 ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.80 Safari/537.36 Edg/98.0.1108.43

Challenge: Cash Register

Link to the challenge:

I never would have thought of naming object keys like you did, i.e

{ 0.01: 'PENNY' }

Although that works, it probably would make more sense to switch the keys and values, and have billArr hold the names of the bills, instead of the values.

Then, instead of cidCopy[billObj[billArr[i]]] -= billArr[i], you could have a temporary variable to hold the value of billArr[i], and it would look like cidCopy[bill] -= billObj[bill], which just looks cleaner to me. But this is just personal preference, and the most important thing is that you finished the project, gaining valuable problem-solving experience. The more familiar you get with js, the more elegant your algorithms will become. Great job, and keep up the good work!

1 Like

Great idea there, would definitely make the algorithm look cleaner. Thank you for the feedback.

1 Like

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