# Cash Register, need to reduce

Cash Register, need to reduce
0

#1

Tell us what’s happening:
I’m able to get the it almost working but i need to reduce the object arr to have only the highest cash value for each dollar amount listed. This is what it gives now:

TWENTY,2000,TWENTY,4000,TWENTY,6000,TEN,7000,TEN,8000,FIVE,8500,FIVE,9000,FIVE,9500,ONE,9600,QUARTER,9625,QUARTER,9650,DIME,9660,DIME,9670,PENNY,9671,PENNY,9672,PENNY,9673,PENNY,9674

``````
var cashValue = [10000, 2000, 1000, 500, 100, 25, 10, 5, 1];
var cashName = ['ONE HUNDRED', 'TWENTY', 'TEN', 'FIVE', 'ONE', 'QUARTER', 'DIME', 'NICKEL', 'PENNY'];

function checkCashRegister(price, cash, cid) {
let tempPrice = price*100;
let tempCash = cash*100;
var tempArr = cid.map(x => {return x[1]*100;});
var change = (cash*100 - price*100), total, arr = cid.map(x => [x[0], x[1]]);
var output = { status: "OPEN", change: [] };
var sum = 0;

total = cid.reduce((tot, inc) => {

tempArr = tempArr.reverse();

if(total < change){
console.log('first if');
return {status: "INSUFFICIENT_FUNDS", change: []};
} else if(total === change){
return {status: "CLOSED", change: cid};
} else if (total > change){
for (let i = 0; i < tempArr.length; i++){
while (change >= cashValue[i] && tempArr[i] > 0){
change -= cashValue[i];
tempArr[i] -= cashValue[i];
sum += cashValue[i];
output.change.push([cashName[i], sum]);
}
}
console.log('this is the chang e ' + change + ' and this is the total ' + total)
if (change > 0){
console.log('we in here');
return {status: "INSUFFICIENT_FUNDS", change: []};
}

//this is the code i'd like to replace with a reduce method
else{
console.log(output.change);
for(let i = 0; i < output.change.length;i++){
if (output.change[i][0] == output.change[i+1][0]){
delete output.change[i];
i++;
}
}

console.log(output.change);
return {status: "OPEN", change:[]};
}
}

// Here is your change, ma'am.
return change;

}

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);
``````

#2

I’m guessing that you want to take the solution that you have and modify the array instead of prevent the dupication in the first place (based on the way you worded your question). How do you know which one you want to keep? If you can articulate the logic by which you know that a given element of the array should be removed, you can filter out the “bad” values.

#3

I need help with the filter method, i have a nested array that has multiple array with the same cash name but different cash values, for exampe:

[[TWENTY,2000],[TWENTY,4000],[TWENTY,6000],[TEN,7000],[TEN,8000]]

I want to filter out all the nested arrays that have the same cash name and only keep the array that has the highest cash value. So using the example above, i only want to return:

[[TWENTY, 6000],[TEN, 8000]]

I think using the filter method would be best but i’m not sure how to implement it, please help!

#4

My question before still stands: how do you know which is the right value to keep?

#5

O, sorry, it needs to keep the largest value for that cash name.

#6

Remember when you did the challenge to return the longest word? You could use similar logic to figure out your correct values.

#7

Ok, thanks i’ll try that!