0

I need my code to return the following:

{
  status: 'OPEN',
  change: [
    ['TWENTY', 60],
    ['TEN', 20],
    ['FIVE', 15],
    ['ONE', 1],
    ['QUARTER', 0.5],
    ['DIME', 0.2],
    ['PENNY', 0.04],
  ],
};

but right now it returns:

{
  status: 'OPEN',
  change: [
    ['TWENTY', 20],
    ['TWENTY', 20],
    ['TWENTY', 20],
    ['TEN', 10],
    ['TEN', 10],
    ['FIVE', 5],
    ['FIVE', 5],
    ['FIVE', 5],
    ['ONE', 1],
    ['QUARTER', 0.25],
    ['QUARTER', 0.25],
    ['DIME', 0.1],
    ['DIME', 0.1],
    ['PENNY', 0.01],
    ['PENNY', 0.01],
    ['PENNY', 0.01],
    ['PENNY', 0.01],
  ],
};

How can I sum all arrays whose first element is the same? I´ve tried using a loop, but it didn't work the way I wanted

  let cashInDrawer = cid;
  let cashInDrawerReverse = cashInDrawer.reverse();
  let currencyUnits = [
     ["ONE HUNDRED", 100],
     ["TWENTY", 20],
     ["TEN", 10],
     ["FIVE", 5],
     ["ONE", 1],
     ["QUARTER", 0.25],
     ["DIME", 0.1],
     ["NICKEL", 0.05],
     ["PENNY", 0.01]
  ];

  let availableMoney = [];
  let totalAvailableMoney = 0;
  let totalChange = cash - price;
  let change = []
  
  //Check the available money:
  for(let index = 0; index < cid.length; index++){
    availableMoney.push(cid[index][1]);
  }

  availableMoney.reverse();

  //Total of available money:
  
  for(let f = 0; f < availableMoney.length; f++){
    totalAvailableMoney += availableMoney[f];
  }

//Returns:

  if(totalChange > totalAvailableMoney){
    return {status: "INSUFFICIENT_FUNDS", change: []}
  }
  else if (totalChange === totalAvailableMoney){
    return {status: "CLOSED", change: cashInDrawer.reverse()}
  } 
  else {
    for(var i = 0; i < cashInDrawerReverse.length; i++){
     if(cashInDrawerReverse[i][1] != 0){
      while(totalChange >= currencyUnits[i][1] && cashInDrawerReverse[i][1] > 0){ 
          change.push(currencyUnits[i]);
          cashInDrawerReverse[i][1] -= currencyUnits[i][1];
          totalChange -=  currencyUnits[i][1];
          totalChange = totalChange.toFixed(2); 
      } 
     }
    }
    if(totalChange == 0){
    return {status: "OPEN", change: change}
    } else {
      return {status: "INSUFFICIENT_FUNDS", change: []}
    }
  }

}


console.log(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]]))```
pilchard
  • 12,414
  • 5
  • 11
  • 23
Valentina
  • 23
  • 2
  • 1
    Looks like you just need to [group and sum](https://stackoverflow.com/questions/68339586/grouping-an-array-and-then-summing-values) by coin type, though it would be simple enough to avoid having to do it afterwords by simply making it an object keyed by coin instead of an array of tuples. – pilchard Jan 15 '23 at 22:07

1 Answers1

0

const change = [['TWENTY', 20],
  ['TWENTY', 20], ['TWENTY', 20], ['TEN', 10], ['TEN', 10], ['FIVE', 5], ['FIVE', 5], ['FIVE', 5], ['ONE', 1], ['QUARTER', 0.25], ['QUARTER', 0.25], ['DIME', 0.1], ['DIME', 0.1], ['PENNY', 0.01], ['PENNY', 0.01], ['PENNY', 0.01], ['PENNY', 0.01]]
  

const result = {}
for (const [denomination, amount] of change) {
  if (!result[denomination]) {
    result[denomination] = amount
  } else {
    result[denomination] += amount
  }
}

console.log(Object.keys(result).map(key => ([key, result[key]])))
ksav
  • 20,015
  • 6
  • 46
  • 66