# JavaScript Algorithms and Data Structures Projects: Cash Register/ Help

**URL:** <https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 8, 2018, 1:00pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950 "2018-09-08T13:00:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OumaimaTiguint](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oumaimatiguint/32/99453_2.png) [@OumaimaTiguint](https://forum.freecodecamp.org/u/OumaimaTiguint)\
**Post date:** [September 8, 2018, 1:00pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950/1 "2018-09-08T13:00:55Z")

</div>

**Tell us what’s happening:**  
I can only pass the first test and I don’t know why

**Your code so far**

```js

const REGISTER_STATUS = {insufficientFunds: "INSUFFICIENT_FUNDS", closed: 
"CLOSED", open: "OPEN"};

function checkCashRegister(price, cash, cid) {
  let cashRegister = {status: '', cash: cid};
  const changeNeeded = parseFloat(cash-price).toFixed(2);
  const changeAvailable = getTotalCashRegisterChange(cid); 
  cashRegister.status = getTotalCashRegisterStatus(changeNeeded, changeAvailable);

  if(cashRegister.status === REGISTER_STATUS.insufficientFunds) {
    cashRegister.change = [];
    return cashRegister;
  }

  cashRegister.change = getCustomersChange(changeNeeded, cid);

  if(changeNeeded > getTotalCashRegisterChange(cashRegister.change)) {
    cashRegister.status = REGISTER_STATUS.insufficientFunds;
    cashRegister.change = [];
  }

  if(cashRegister.status === REGISTER_STATUS.closed) {
    cashRegister.change = [...cid];
  }
  
  return cashRegister;
}

function getCustomersChange(changeNeeded, changeInDrawer) {
  const change = [];
  const currencyDictionary = {
    "PENNY": 0.01,
    "NICKEL": 0.05,
    "DIME": 0.10,
    "QUARTER": 0.25,
    "ONE": 1.00,
    "FIVE": 5.00,
    "TEN": 10.00,
    "TWENTY": 20.00,
    "ONE HUNDRED": 100.00
  };

  for(let i = changeInDrawer.length-1; i>=0; i--) {
    const coinName = changeInDrawer[i][0];
    const coinTotal = changeInDrawer[i][1];
    const coinValue = currencyDictionary[coinName];
    let coinAmount = (coinTotal / coinValue).toFixed(2);
    let coinsToReturn = 0;

    while(changeNeeded >= coinValue && coinAmount > 0) {
      changeNeeded -= coinValue;
      changeNeeded = changeNeeded.toFixed(2);
      coinAmount--;
      coinsToReturn++;
    }

    if (coinsToReturn > 0) {
      change.push([coinName, coinsToReturn * coinValue]);
    }

  }

  return change;
}

function getTotalCashRegisterChange(changeInDrawer) {
  let total = 0;
  for(let change in changeInDrawer) {
    let changeValue = change[1];
    total += changeValue;
  }
  return total.toFixed(2);
}

function getTotalCashRegisterStatus(changeNeeded, changeAvailable) {
  if(Number(changeNeeded)> Number(changeAvailable)) {
    return REGISTER_STATUS.insufficientFunds;
  } else if (Number(changeNeeded) < Number(changeAvailable)) {
    return REGISTER_STATUS.open;
  } else {
    return REGISTER_STATUS.closed;
  }
}

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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36`.

**Link to the challenge:**

> **[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

---

<div class="post-metadata">

**Author:** ![zapcannon99](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zapcannon99/32/94297_2.png) [@zapcannon99](https://forum.freecodecamp.org/u/zapcannon99)\
**Post date:** [September 8, 2018, 1:20pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950/2 "2018-09-08T13:20:20Z")

</div>

It might help you to have console.log() line somewhere you think would be useful. The result I get from the first test is closed. It doesn’t match what the answer is.

Since that is the case, the problem lies either with how you’re handling the cases, or the way your calculating the tender used, or even both.

---

<div class="post-metadata">

**Author:** ![OumaimaTiguint](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/oumaimatiguint/32/99453_2.png) [@OumaimaTiguint](https://forum.freecodecamp.org/u/OumaimaTiguint)\
**Post date:** [September 10, 2018, 1:15pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950/3 "2018-09-10T13:15:58Z")

</div>

Thanks for your help! I figured it out

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [February 2, 2021, 11:31am UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/224950/4 "2021-02-02T11:31:26Z")

</div>


