Cash Register, need to reduce

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:


i just want TWENT,6000,TEN,8000, etc, please help!

Your code so far

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 = => {return x[1]*100;});
  var change = (cash*100 - price*100), total, arr = => [x[0], x[1]]);
  var output = { status: "OPEN", change: [] };
  var sum = 0;

  total = cid.reduce((tot, inc) => {
    return tot+(inc[1]*100);},0)

    tempArr = tempArr.reverse();

  if(total < change){
    console.log('first if');
    return {status: "INSUFFICIENT_FUNDS", change: []};
  } else if(total === change){
    console.log('we made it');
    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
        for(let i = 0; i < output.change.length;i++){
        if (output.change[i][0] == output.change[i+1][0]){
          delete output.change[i];
    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]]);

Link to the challenge:

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.

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:


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!

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

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

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

Ok, thanks i’ll try that!