Converting an array into object

**Hello everyone,
I’d need to clarify the code that converts an array into object (code bellow). I get how it works…sort of, but my understanding of it is less than crystally clear.

For example, what exactly parameters function(acc, curr) refer to?
Furthermore, why is it coded acc[curr[0]] = curr[1];?
Should it not be acc[curr[1]] = curr[1];?

Help is much appreciated.

Your code so far

// Create an array of objects which hold the denominations and their values
... this  part is clear....

// questions refer to the part bellow

function checkCashRegister(price, cash, cid) {
  var output = { status: null, change: [ ] };
  var change = cash - price;

  // Transform CID array into drawer object
  var register = cid.reduce(
    function(acc, curr) { += curr[1];
      acc[curr[0]] = curr[1];
      return acc;
    { total: 0 }

//.... code goes on...   

function checkCashRegister(price, cash, cid) {
var change;
return change;

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

the reduce method gives to the callback two arguments, an accumulator that is updated each time with the value returned from the callback, and the current value being iterated over

incide the array there are other arrays, in these sub-arrays you have a string as first element and a number as second one
so if the subarray is ["PENNY", 0.17] inside the object there will be a key-value pair as "PENNY": 0.17
you are suggesting for the key-value pair to be 0.17: 0.17 which wouldn’t be much useful, would it?

1 Like