Result of checkCashRegister activity


This is my result for the activity:

function checkCashRegister(price, cash, cid) {
    ["ONE HUNDRED", 100],
    ["TWENTY", 20],
    ["TEN", 10],
    ["FIVE", 5],
    ["ONE", 1],
    ["QUARTER", 0.25],
    ["DIME", 0.1],
    ["NICKEL", 0.05],
    ["PENNY", 0.01],
  const [WORD, NUMBER] = [0, 1];

  let change = Math.abs(cash - price);
  let cashInDrawer;
  let result;

  // Calculate total cash in drawer
  cashInDrawer = cid.reduce((sum, amt) => (sum += amt[NUMBER]), 0);

  if (cashInDrawer == change) return { status: "CLOSED", change: cid };

  // Make cid item order align with that of CURRENCY_TRANSLATION so that calculations can be made later;

  result = CURRENCY_TRANSLATION.reduce((acc, currency, index) => {
    let isDivisible = () => !!Math.trunc(change / currency[NUMBER]);
    let currencyTypeInTill = () => cid[index][NUMBER] > 0;

    let currencyCount = 0;
    while (isDivisible() && currencyTypeInTill()) {
      change -= currency[NUMBER];
      // round answer to two decimal places. Otherwise, it goes berserk as it looses precision.
      change = change.toFixed(2);
      cid[index][NUMBER] -= currency[NUMBER];
      currencyCount += currency[NUMBER];

    if (currencyCount) acc.push([currency[WORD], currencyCount]);

    return acc;
  }, []);

  // If change could not be zero'ed, then there was not enough funds, or exact change available
  if (change != 0) {
    return { status: "INSUFFICIENT_FUNDS", change: [] };
  } else {
    return { status: "OPEN", change: result };

  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],

One of the main challenges that I find myself running into as I write code to perform more complex tasks, is keeping the code from being too obscure. I think this comes over from the days when I used to dawdle with Perl, with a focus on writing as few lines as possible. I now know that this is not a good idea though (neither for teamwork nor for maintenance).

But the question still remains, ‘how obscure is too obscure’? How do you draw the line between concise/not bloated and obscure, and how do you catch yourself from doing so? Is there any articles that you suggest?

1 Like

There are as many option ‘clear code’ as there are developers. I’d say that you are doing pretty good by caring about it is the first place. Clear variable names with words and the simplest logic that works will get you pretty far.


Thank you for the feedback :+1:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.