Cash Register register[curr.name]

Tell us what’s happening:
I’m rewriting my solution to this project and have some questions about Solution 1. I wrote questions as comments to help make things more clear.
I guess its not weird to put var denom outside of the function because its an object. Doing so allows you to use it for other functions. It also seems that the name and val keys facilitate the ability to access its information. My rewrite uses similar code for the register object but I am struggling with how to make full use of its data because if I write for example drawer[cur[1] outside of the } i get TypeError: Cannot read property ... of undefined. Is it because var denom has name and val keys that you can then apply them to register? that’s, it isn’t it? same way I can loop through an array with var i = 0 then reference otherArray[i]? Thanks for any insight you can provide on this.

Your code so far


// why is this declared before the function? 
var denom = [
{ name: "ONE HUNDRED", val: 100.0 },
{ name: "TWENTY", val: 20.0 },
{ name: "TEN", val: 10.0 },
{ name: "FIVE", val: 5.0 },
{ name: "ONE", val: 1.0 },
{ name: "QUARTER", val: 0.25 },
{ name: "DIME", val: 0.1 },
{ name: "NICKEL", val: 0.05 },
{ name: "PENNY", val: 0.01 }
];


// Is the purpose of this only to provide the total?
var register = cid.reduce(
  function(acc, curr) {
    acc.total += curr[1];
    acc[curr[0]] = curr[1];
    return acc;
  },
  { total: 0 }
);





var change_arr = denom.reduce(function(acc, curr) {
  var value = 0;
  
  while (register[curr.name] > 0 && change >= curr.val) {
    change -= curr.val;
//how is it letting you reference the register var
//outside of the register function???
    register[curr.name] -= curr.val;
    value += curr.val;
  }
     if (value > 0) {
    acc.push([curr.name, value]);
  }


Your browser information:

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

Challenge: Cash Register

Link to the challenge: