# Criticize my solution \[Cash Register\]

**URL:** <https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 23, 2021, 10:56am UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938 "2021-02-23T10:56:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [February 23, 2021, 10:56am UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/1 "2021-02-23T10:56:46Z")

</div>

Here is the problem is known as [Cash Register](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register).  
I solved this and got a [certificate](https://www.freecodecamp.org/certification/plabonkumersarker/javascript-algorithms-and-data-structures) 😌 🥳  
But, my code is pretty big.  
Please suggest to me a better way to fix this, if you have some.  
Here is my code below,

```auto
function checkCashRegister(price, cash, cid) {
  let x = cash-price;
  let budget = {};
  for(let i=0; i<cid.length; i++){
    budget[cid[i][0]] = cid[i][1];
  }
  let result =[];
  let eqObj = {status: "CLOSED", change: cid};
  let insaObj = {status: "INSUFFICIENT_FUNDS", change: []};
  let valBug = Object.values(budget);
  let sum = 0;
  for(let i=0; i<valBug.length; i++){
    sum += valBug[i];
  }
  if(x===sum){
    return eqObj;
  }else if(x>sum){
    return insaObj
  }else{
    if(x>100){
      let c = Math.min(Math.floor(x/100),Math.floor(budget["ONE HUNDRED"]/100));
      if(budget["ONE HUNDRED"] >= c && c!= 0){
        result.push(["ONE HUNDRED", c*100]);
        x = (x-(c*100)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>20){
      let c = Math.min(Math.floor(x/20),Math.floor(budget["TWENTY"]/20));
      if(budget["TWENTY"] >= c && c!= 0){
        result.push(["TWENTY", c*20]);
        x = (x-(c*20)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>10){
      let c = Math.min(Math.floor(x/10),Math.floor(budget["TEN"]/10));
      if(budget["TEN"] >= c && c!= 0){
        result.push(["TEN", c*10]);
        x = (x-(c*10)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>5){
      let c = Math.min(Math.floor(x/5),Math.floor(budget["FIVE"]/5));
      if(budget["FIVE"] >= c && c!= 0){
        result.push(["FIVE", c*5]);
        x = (x-(c*5)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>1){
      let c = Math.min(Math.floor(x/1),Math.floor(budget["ONE"]/1));
      if(budget["ONE"] >= c && c!= 0){
        result.push(["ONE", c*1]);
        x = (x-(c*1)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>0.25){
      let c = Math.min(Math.floor(x/0.25),Math.floor(budget["QUARTER"]/0.25));
      if(budget["QUARTER"] >= c && c!= 0){
        result.push(["QUARTER", c*0.25]);
        x = (x-(c*0.25)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>0.10){
      let c = Math.min(Math.floor(x/0.10),Math.floor(budget["DIME"]/0.10));
      if(budget["DIME"] >= c && c!= 0){
        result.push(["DIME", c*0.10]);
        x = (x-(c*0.10)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x>0.05){
      let c = Math.min(Math.floor(x/0.05),Math.floor(budget["NICKEL"]/0.05));
      if(budget["NICKEL"] >= c && c!= 0){
        result.push(["NICKEL", c*0.05]);
        x = (x -(c*0.05)).toFixed(2);
      }else{
        return insaObj;
      }
    }
    if(x > 0.01){
      let c = Math.min(Math.floor(x/0.01),Math.floor(budget["PENNY"]/0.01));
      if(budget["PENNY"] >= c*0.01 && c!= 0){
        result.push(["PENNY", c*0.01]);
        x = (x - (c*0.01)).toFixed(2);
      }else{
        return insaObj;
      }
    }
  }
  return {status: "OPEN", change: result};
}

```

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [February 23, 2021, 5:20pm UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/2 "2021-02-23T17:20:54Z")

</div>

Hi @Plabon_Kumer_Sarker !

Congrats on getting the javascript certification!

I moved your posts out of project feedback and into the javascript section because you might get more feedback here.

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [February 23, 2021, 5:33pm UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/3 "2021-02-23T17:33:06Z")

</div>

My main suggestion would be to fix some of these variable names.

> [@Plabon\_Kumer\_Sarker](#):
>
> ` let x = cash-price;`

One letter variable names are not that descriptive.  
Especially when you get into building longer projects it gets confusing if you are using names like x,y,w,etc.

Picking more descriptive names will help people looking at your code know exactly what that variable is supposed to do.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [February 23, 2021, 5:52pm UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/4 "2021-02-23T17:52:31Z")

</div>

You have a lot of repetition in your code, which is usually a sign that there’s room for optimisation. All those `if` blocks do essentially the same, only with different parameters, so I’d probably write a function which does all that, and call it with different arguments depending on the `if` condition.

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [February 24, 2021, 5:52am UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/5 "2021-02-24T05:52:14Z")

</div>

@jsdisco Did you mean this? It’s really shorter than before 😌

```auto
function checkCashRegister(price, cash, cid) {
  let x = cash-price;
  let budget = {};
  for(let i=0; i<cid.length; i++){
    budget[cid[i][0]] = cid[i][1];
  }
  let result =[];
  let eqObj = {status: "CLOSED", change: cid};
  let insaObj = {status: "INSUFFICIENT_FUNDS", change: []};
  let valBug = Object.values(budget);
  let keyBug = (Object.keys(budget)).reverse();
  let allCurr = [100,20,10,5,1,0.25,0.10,0.05,0.01];
  let sum = 0;
  for(let i=0; i<valBug.length; i++){
    sum += valBug[i];
  }
  if(x===sum){
    return eqObj;
  }else if(x>sum){
    return insaObj
  }else{
    for(let i=0; i<keyBug.length; i++){
      if(x>allCurr[i]){
        let c = Math.min(Math.floor(x/allCurr[i]),Math.floor(budget[keyBug[i]]/allCurr[i]));
        if(allCurr[i] === 0.01){
          if(budget[keyBug[i]] >= c*0.01 && c!= 0){
            result.push([keyBug[i], c*allCurr[i]]);
            x = (x-(c*allCurr[i])).toFixed(2);
          }else{
            return insaObj;
          }
        }else{
          if(budget[keyBug[i]] >= c && c!= 0){
            result.push([keyBug[i], c*allCurr[i]]);
            x = (x-(c*allCurr[i])).toFixed(2);
          }else{
            return insaObj;
          }
        }
      }
    }
  }
  return {status: "OPEN", change: result};
}

```

---

<div class="post-metadata">

**Author:** ![Plabon\_Kumer\_Sarker](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Plabon\_Kumer\_Sarker](https://forum.freecodecamp.org/u/Plabon_Kumer_Sarker)\
**Post date:** [February 24, 2021, 5:57am UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/6 "2021-02-24T05:57:20Z")

</div>

@jwilkins.oboe Thank you 🤗 I will try to use descriptive variable names next time.

---

<div class="post-metadata">

**Author:** ![jsdisco](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jsdisco/32/179655_2.png) [@jsdisco](https://forum.freecodecamp.org/u/jsdisco)\
**Post date:** [February 24, 2021, 9:21am UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/7 "2021-02-24T09:21:58Z")

</div>

Yeah that’s a lot more readable. As for variable names, I’d probably rename `keyBug` and `valBug` to `keyBudg` and `valBudg` or similar, so they don’t indicate that you’re collecting bugs here 😃

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 25, 2021, 9:22pm UTC](https://forum.freecodecamp.org/t/criticize-my-solution-cash-register/447938/8 "2021-08-25T21:22:22Z")

</div>

Closed on August 25, 2021, 9:22pm UTC

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