Cash Register - challenge & pushing through

Cash Register - challenge & pushing through
0

#1

Tell us what’s happening:
I am going about the solution slightly differently than the “Get a Hint” solution.
I’ve had challenges with more complex topics, such as variable arrays, and Loops. So, I wanted to just glance at how the “Get a Hint” solution was solving, just to get an idea.

I am having a bit of a problem understanding what goes after the dot in the following:
cid.reduce(function(acc, curr)
register.total === change
register[curr.name] > 0 && change >= curr.val
output.status = ‘CLOSED’ ==> this one seems to make sense, as I see the variable declared: var output = { status: null, change: [] };

Can you maybe refer me to a lesson(s) that talks about .reduce, .total, curr.name? Is .reduce a math operator?

I am thinking of using a % mod operator to count the bills and coins. I am having a hard time understanding how the “Get a Hint” solution counts out these bills and coins. I feel a bit lost in the sea of dot.words and the many variables being used.

Thank you.

Your code so far


var price;
var cash;
var cid;

function checkCashRegister(price, cash, cid) {
  var change;
  var status;
  change=cash-price; // cash paid to cashier minus price equals change to return to customer
  var moneyLeftInDrawer=cid[1]-change; // moneyLeftInDrawer will determine the "status" (insuf funds, closed, open)
  if (moneyLeftInDrawer<0){ 
    return {status: "INSUFFICIENT_FUNDS", change: []};
  } else if (moneyLeftInDrawer=0){
    return {status:"CLOSED", change: []};
    } else if (moneyLeftInDrawer>0){
      return {status:"OPEN", change: []};
      }

var PENNY, NICKEL, DIME, QUARTER, ONE, FIVE, TEN, TWENTY, ONE HUNDRED;

/* 
--- work in progress below --- :)

  return change (return in coins and bills); >>

      if change coins and bills include >> PENNY, NICKEL, DIME, QUARTER, ONE, FIVE, TEN, TWENTY, ONE HUNDRED (ex. 2.05 == 2 ONE's, 1 NICKEL -- add "Dollar Bill(s)" after reporting bills; 

BILLS: 
0-4 >> return ONE "Dollar Bill(s)"
5 >> return FIVE
6-9 >> return 1 FIVE and subtract five to obtain number of ONE Dollar Bills to return
10 >> return TEN
suppose you have $121.72 to return change to customer.
use %100 will rtn 1; >> if %100 >0 store result in HUNDRED variable & then return HUNDRED + "Dollar Bill(s)";
then subtract HUNDRED*100 from change;
use %20 will rtn 1; >> if %20>0 store results in TWENTY variable & then return TWENTY + "Dollar Bill(s)";
usee %10 ... (continue above process)
use %5
use %1
use %0.25
use %0.10
use %0.05
use %0.01

note: change will become an object/var array of coins and bills.
e.g. obj or var change (){
HUNDRED: amount,
TWENTY: amount,
TEN: amount,
...

--- WORK IN PROGRESS ABOVE ---
*/
}


}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 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

Search for array methods like reduce here.


#3

This should help you. Also if you like me that even if you see solution and don’t want to just copy it to pass tests, here’s mine solution, read comments in js code and you’ll get an idea what you’ll need to know prior to actually addressing the issue.


#4

There is so much going on in your code. I am lost. Am I on the right track towards solving?


#5

You need t check four things: If there’s not enough money, If it’s same amount as in cashreg, or if cashreg have more then change. That last one can go both ways. If you for example have 1.01$(in bills: one dollar and one nickel) in cashreg but your change is 0.5, you would also say “closed”. To calculate any of these calculations, you need to determine lower bills that can be used i.e for example 1.01$ it is 1$, then its 0.25$, 0.0.5% and so on depends on bills you have.

  1. You got change 1.01
  2. Determine for which bills its divisible: 1, 0.25, 0.05, 0.01
  3. Start dividing. You need to take a notice how much times you need to divide. If you have 0.75$ in quarters, then you can make three divisions with 0.25.
  4. When you can’t divide with certain bill anymore move to the next in line, next descending and divide with ‘it’.
  5. All of that time you also need to track which bills you used to divide(denomination/banknote) and it’s name i.e “quarter” and push it into prepared object you have with appropriate ‘status’.
  6. After you reach 0 “zero” return said object.
  7. If you can’t reach zero by not having anything to divide your change, than even having more than enough money in cashreg, but not in appropriate bills you should return you object with status closed and empty array of bills.
    More or less is explained in comments of my code.

#6

Wow, I did not think of that. Thank you for that insight!