Cach Regster help

Hello everyone, please help me with my code. I can’t understand what is wrong with my ‘newResult’ array.

var denom = [

  { name: "ONE HUNDRED", value: 100.0 },

  { name: "TWENTY", value: 20.0 },

  { name: "TEN", value: 10.0 },

  { name: "FIVE", value: 5.0 },

  { name: "ONE", value: 1.0 },

  { name: "QUARTER", value: 0.25 },

  { name: "DIME", value: 0.1 },   

  { name: "NICKEL", value: 0.05 },

  { name: "PENNY", value: 0.01 }

];

function checkCashRegister(price, cash, cid) {

  let change = cash - price;

  let result = {

    status: "",

    change: []

  }

  let sumCid = 0;

  for(let element of cid) {

  sumCid += element[1];

  }

  if(sumCid < change) {

    result.status = "INSUFFICIENT_FUNDS";

    return result;

  }

  if(sumCid === change) {

    result.status = "CLOSED"

    result.change = cid;

    return result;

  }

  let newResult = [];

  cid = cid.reverse();

  let val = 0;

  for(let i = 0; i < cid.length; i++) {

   while(cid[i][1] > 0 && change >= denom[i].value) {

  

   change -= denom[i].value

   change = change.toFixed(2);

   cid[i][1] -= denom[i].value

   val += denom[i].value;

   }

   if(val > 0) {

   newResult.push([cid[i][0], val])

   }

  }

result.status = "OPEN";

result.change = newResult;

return result;

}

console.log(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]]));

Hi and welcome to the forum.

Can you expand a bit more about the issue you are having? How is your newResult array not matching your expectations?


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Hi!
‘newResult’ looks like: [ [ 'QUARTER', 0.5 ], [ 'DIME', 0.5 ], [ 'NICKEL', 0.5 ], [ 'PENNY', 0.5 ] ]
but i need [["QUARTER", 0.5]]. Can you tell me please , what’s wrong in this part of my code:

let newResult = [];
  cid = cid.reverse();
  let val = 0;
  for(let i = 0; i < cid.length; i++) {
   while(cid[i][1] > 0 && change >= denom[i].value) {
  
   change -= denom[i].value
   change = change.toFixed(2);
   cid[i][1] -= denom[i].value
   val += denom[i].value;
   }
   if(val > 0) {
   newResult.push([cid[i][0], val])
   }
  }
return newResult;