# JavaScript Algorithms and Data Structures Projects: Cash Register Help

**URL:** <https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [May 15, 2020, 7:11am UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174 "2020-05-15T07:11:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![DebWesigner](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/debwesigner/32/136183_2.png) [@DebWesigner](https://forum.freecodecamp.org/u/DebWesigner)\
**Post date:** [May 15, 2020, 7:11am UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174/1 "2020-05-15T07:11:15Z")

</div>

Hello,

For the Cash Register problem, I was focusing on what I thought to be the most difficult part, which was being able to calculate what kind of change and how much I can return according to the CID.

For example, if I have to give back 1 dollar change, but I only have enough pennies, I should be able to return the change in pennies.

I finally got it to work, but instead of the array giving the total sum of the dollar value, I am getting each dollar value pushed into the array.

For example: I have to return 0.5 in change. My function returns:  
[[ ‘QUARTER’, 0.25], [‘QUARTER’, 0.25] ]

Instead of

[[ ‘QUARTER’, 0.5]]

I’m wondering how I can return the sum of the change if it is the same dollar value

Here’s my code so far (apologies for poor formatting and potential pointless code):

```auto
function checkCashRegister(price, cash, cid) {
 
let currency = {
"ONE HUNDRED": 100,
"TWENTY":20,
"TEN": 10,
"FIVE": 5,
"ONE": 1,
"QUARTER":0.25,
"DIME": 0.1,
"NICKEL": 0.05,
"PENNY": 0.01,
}

//calculating change
let change = cash -price;

//function to find total of cash in drawer
let cidfun = cid.reduce((acc, num)=>{
return acc +=num[1];
},0)

//because js sucks at maths
let totalCID= cidfun.toFixed(2);

let changeArray=[]

//calculating the smallest values I can use to give back change and putting into an array
let fachange = change;
for (let key in currency){
if (fachange>currency[key]){
changeArray.push(key);
}
}

//the array for the total change I the CID is able to give
let resArr = [];

//function to return correct change if cid is able
cid=cid.reverse()

cid.map(val => {

if (val[1]> change){

while (val[1]>0 && change>=currency[val[0]]){

val[1]-=currency[val[0]]
change-= currency[val[0]]

resArr.push([val[0],currency[val[0]]])

}
}
})

console.log(resArr)

let result = {"status": "OPEN", "change": resArr}

}

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]]);

```

---

<div class="post-metadata">

**Author:** ![arjaykun](https://avatars.discourse-cdn.com/v4/letter/a/ccd318/32.png) [@arjaykun](https://forum.freecodecamp.org/u/arjaykun)\
**Post date:** [May 15, 2020, 7:45am UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174/2 "2020-05-15T07:45:17Z")

</div>

it’s because you’re pushing your output in the resArr inside while loop;

---

<div class="post-metadata">

**Author:** ![DebWesigner](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/debwesigner/32/136183_2.png) [@DebWesigner](https://forum.freecodecamp.org/u/DebWesigner)\
**Post date:** [May 15, 2020, 12:04pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174/3 "2020-05-15T12:04:15Z")

</div>

I figured it out before the reply. Thanks anyway! What an airhead moment of mine.

---

<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:** [June 1, 2021, 12:54pm UTC](https://forum.freecodecamp.org/t/javascript-algorithms-and-data-structures-projects-cash-register-help/389174/4 "2021-06-01T12:54:09Z")

</div>

Closed on June 1, 2021, 12:54pm UTC


