# \[Javascript\] Cash register project

**URL:** <https://forum.freecodecamp.org/t/javascript-cash-register-project/467642>\
**Category:** I Built This\
**Created:** [July 3, 2021, 11:35am UTC](https://forum.freecodecamp.org/t/javascript-cash-register-project/467642 "2021-07-03T11:35:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpeyron](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tpeyron/32/221587_2.png) [@tpeyron](https://forum.freecodecamp.org/u/tpeyron)\
**Post date:** [July 3, 2021, 11:35am UTC](https://forum.freecodecamp.org/t/javascript-cash-register-project/467642/1 "2021-07-03T11:35:40Z")

</div>

Hi there,

I would like to have some feedback on my solution for the cash register project. Here is the link to the project :

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register)**
>
> Learn to Code — For Free

And here is my code :

```auto
function checkCashRegister(price, cash, cid) {
  //initialization of the status and the change array
  var status="";
  var change=[
  ["PENNY", 0],
  ["NICKEL", 0],
  ["DIME", 0],
  ["QUARTER", 0],
  ["ONE", 0],
  ["FIVE", 0],
  ["TEN", 0],
  ["TWENTY", 0],
  ["ONE HUNDRED", 0]
];
  //computation of the due change and total cash in CID
  var changeDue = toPrecise(cash-price);
  var totalCash=toPrecise(cid.map(element => element[1]).reduce((a,b) =>a+b));
  //Exit case : if there isn't enough money in CID
  if(toPrecise(changeDue-totalCash)>0){
    return {status:"INSUFFICIENT_FUNDS", change:[]};

  } else{
    //for each type of money...
    for(let i=cid.length-1;i>=0; i--){
      //while there is still money in the cid, change to give, and change - current money is positive
        while(cid[i][1]>0
        &&changeDue>0
        &&toPrecise(changeDue-currencyToAmount(cid[i][0]))>=0){
          change[i][1]=change[i][1]+currencyToAmount(cid[i][0]);
          cid[i][1]=toPrecise(cid[i][1]-currencyToAmount(cid[i][0]));
          changeDue=toPrecise(changeDue-currencyToAmount(cid[i][0]));
        }
      }
    }
    //re computation of the total cash in CID
    totalCash=cid.map(element => element[1]).reduce((a,b) =>a+b);
    //If there is still change due after :
    if(changeDue>0&&totalCash!=0){
      return {status:"INSUFFICIENT_FUNDS", change:[]};
    //If all the cash is given :
    } else if(toPrecise(changeDue-totalCash)==0){
      return {status:"CLOSED",change:change
    .map(element => [element[0], parseFloat(toPrecise(element[1]))])};
    //If there is still cash in the cid after giving the change :
    } else{
        return {status:"OPEN", change:change.filter(element => element[1]!=0).reverse()};
    }
}

function toPrecise(expression){
  return expression.toFixed(2);
}

function currencyToAmount(name){
  switch(name){
    case "PENNY":	
      return 0.01;
      break;
    case "NICKEL":	
      return 0.05;
      break;
    case "DIME":	
      return 0.1;
      break;
    case "QUARTER":	
      return 0.25;
      break;
    case "ONE":
      return 1;
      break;
    case "FIVE":	
      return 5;
      break;
    case "TEN":	
      return 10;
      break;
    case "TWENTY":
      return 20;
      break;
    case "ONE HUNDRED": 
      return 100;
      break;
  }
}

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

```

If you prefer, I put in in a [codepen.io](http://codepen.io) to play and test it :  
[https://codepen.io/tpeyron/pen/PomPbVw?editors=0010](https://codepen.io/tpeyron/pen/PomPbVw?editors=0010)

I really struggled with the float representation. I’ve followed the advices given in this topic :

> [@Final JS Exercise: Please Criticize My Code](https://forum.freecodecamp.org/t/final-js-exercise-please-criticize-my-code/348921/5):
>
> First of all, you want to separate “global” logic from “local” logic, for instance function that deals with currencies and operations with currencies, as it “globally” applies to any “local” function that you have (or might have in future). Additionally, if you feel that your “local” logic is a bit bulky - too many if...else for example, you normally always can break it down in few “local” functions/modules. Generally it’s healthier and easier to debug. Regarding your approach with currencies,…

But I found that the `currencify` function can’t be used everywhere, and `.toFixed(2)` as well.

I would very much appreciate to have some feedback about where to use or not  
`.toFixed(2)` !

Cheers,

---

<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:** [January 1, 2022, 11:35pm UTC](https://forum.freecodecamp.org/t/javascript-cash-register-project/467642/2 "2022-01-01T23:35:51Z")

</div>

Closed on January 1, 2022, 11:35pm UTC

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