Cash Register- Please help!~~~

Cash Register- Please help!~~~
0.0 0

#1

Tell us what’s happening:

Your code so far

As a non-native English speaker, I’ve been trying to understand both the logic and English about this challenge for few days.
This is the sample code from FCC

var denom = [
  { name: "ONE HUNDRED", val: 100 },
  { name: "TWENTY", val: 20 },
  { name: "TEN", val: 10 },
  { name: "FIVE", val: 5 },
  { name: "ONE", val: 1 },
  { name: "QUARTER", val: 0.25 },
  { name: "DIME", val: 0.1 },
  { name: "NICKEL", val: 0.05 },
  { name: "PENNY", val: 0.01 }
];

function checkCashRegister(price, cash, cid) {
  var output = { status: null, change: [] };
  var change = cash - price;
  var register = cid.reduce(
    function(acc, curr) {
      acc.total += curr[1];
      acc[curr[0]] = curr[1];
      return acc;
    },
    { total: 0 }
  );
  if (register.total === change) {
    output.status = "CLOSED";
    output.change = cid;
    return output;
  }
  if (register.total < change) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    while (register[curr.name] > 0 && change >= curr.val) {
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;
      change = Math.round(change * 100) / 100;
    }
    if (value > 0) {
      acc.push([curr.name, value]);
    }
    return acc;
  }, []);
  if (change_arr.length < 1 || change > 0) {
    output.status = "INSUFFICIENT_FUNDS";
    return output;
  }
  output.status = "OPEN";
  output.change = change_arr;
  return output;
}

Below is the part that I don’t really understand,
First, why do we need to use denom object???
Second, so the reduce function here started with an empty arry [ ] at the very beginning
but what will register[curr.name] be?? I tried to use console.log to see but didn’t get anything.
Basically, I can’t figure out the following four lines…
(I guess it’s because I don’t know the usage of denom object)
If anyone can kindly explain this to me I would be very appreciated.

var change_arr = denom.reduce(function(acc, curr) {
    var value = 0;
    while (register[curr.name] > 0 && change >= curr.val) {
      
      change -= curr.val;
      register[curr.name] -= curr.val;
      value += curr.val;
      change = Math.round(change * 100) / 100;
      
    }
    if (value > 0) {
      acc.push([curr.name, value]);
    }
    return acc;
  }, []);

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register


#2

First, why do we need to use denom object???

It’s not an object, it’s an array where its elements are objects

We use this to store information about the coins, each name and value. The name is shorthand for denominations but personally I think it’d be less confusing to call it coinInformation or something

Second, so the reduce function here started with an empty arry [ ] at the very beginning but what will register[curr.name] be?

reduce takes in a function as its first argument. This function is a function of two arguments, one traditionally called the accumulator (and in this case called acc) , and the other representing the current element being considered (here called curr)

This function travels through the array and essentially does the following line:
acc = f(acc, curr);

What should acc be for the first element? that’s the second argument to reduce, the empty array. This is just the starting value for acc

curr as mentioned before represents the current element of the array, so it’ll first be { name: "ONE HUNDRED", val: 100 }, and then be { name: "TWENTY", val: 20 } etc…

The above explanation might be a little confusing, it’s a hard one to explain.

My advice would be to read more about how reduce works and then practise small examples with it until it ‘clicks’ - it’ll seem so obvious after a short while, and it’s a very important thing to understand
`


#3

Thank you very much. I have a better understanding of reduce
What I don’t get is that since we already have cid, why do we still need to store the coin information?
In my very simple brain I think that I only need to calculate cash in the drawer, sum up it and subtract the change.


#4

Suppose someone needs to get 3 cents change but we only have quarters or above, we can’t pay them their change

We consider the coins separately for cases like that, and we hardcode coin names and their respective values as data we can use when solving the question of whether we can pay their change or not

Does this help?

Btw you shouldn’t be down on yourself, you don’t have a stupid brain and you’re asking the right sort of questions


#5

It’s just I was not good at math at all when I was in school. That’s why I don’t feel confident when it comes to counting, logic or anything related.
But thank you for answering my question, feel like I am able to solve it soon =)