Cash Register Problem | Javascript Projects

Hi,

The issue I am facing is that I have multiple values of accumulated currencies in 2d lists in the array of statObj than having just one value name and accumulating currency value.

Can someone please suggest me how to remove value and keep the arrays of currency with highest value.

Hereby is my code:

function checkCashRegister(price, cash, cid) {
  const originalCid = cid.map((el) => [...el]);
  let currency = [
    ["PENNY", 0.01],
    ["NICKEL", 0.05],
    ["DIME", 0.1],
    ["QUARTER", 0.25],
    ["ONE", 1],
    ["FIVE", 5],
    ["TEN", 10],
    ["TWENTY", 20],
    ["ONE HUNDRED", 100]
  ];
  let change = cash - price;

  let statObj = {
    status: "",
    change: []
  };
  let totalSum = 0;
  let countIndex = 0;

  function updateChange() {
    debugger;
    let usableCurrencyArr = [];
    let closestVal;

    for (let i = 0; i < currency.length; i++) {
      if (currency[i][1] <= change && cid[i][1] > 0) {
        totalSum += cid[i][1];
        usableCurrencyArr.push(currency[i][1]);
        closestVal = Math.max(...usableCurrencyArr);
      }
    }
    for (let i = 0; i < currency.length; i++) {
      console.log(closestVal);
      if (closestVal === currency[i][1]) {
        console.log(i);
        return i;
      }
    }
  }

  console.log("index:", countIndex);
  while (change > 0) {
    let matchedIndex = updateChange();

    if (totalSum < change) {
      console.log(totalSum < change);
      statObj.status = "INSUFFICIENT_FUNDS";
      console.log(statObj);
      return statObj;
    }
    if (change === cid[matchedIndex][1]) {
      cid[matchedIndex][1] = cid[matchedIndex][1] - change;
      console.log(cid[matchedIndex][1], originalCid[matchedIndex][1]);

      if (cid.every((el) => el[1] === 0)) {
        statObj.status = "CLOSED";
        statObj.change = originalCid;
        console.log(statObj);
        return statObj;
      }
    }
    if (currency[matchedIndex][0] === cid[matchedIndex][0]) {
      console.log(currency[matchedIndex][0] === cid[matchedIndex][0]);

      cid[matchedIndex][1] = parseFloat(
        (cid[matchedIndex][1] - currency[matchedIndex][1]).toFixed(2)
      );
      change = parseFloat((change - currency[matchedIndex][1]).toFixed(2));
      console.log(change);
      statObj.status = "OPEN";

      statObj.change.push([
        cid[matchedIndex][0],
        parseFloat(
          (originalCid[matchedIndex][1] - cid[matchedIndex][1]).toFixed(2)
        )
      ]);

      console.log(statObj);
      updateChange();
    }

    //change = change - currency[matchedIndex][1]
  }

  return statObj;
}

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

Following is the link to the project problem - https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register

Thanks!!!

So you need instead of this:

{ status: 'OPEN', change: [ [ 'QUARTER', 0.25 ], [ 'QUARTER', 0.5 ] ] }

get this:

{ status: 'OPEN', change: [ [ 'QUARTER', 0.5 ] ] }

Just making sure.

First idea would be:
just grab array:
statObj.change and reduce() the hell out of it.

But the thing is: I have no idea how it would affect the rest of your logic. That’s a lot of code.

Update.
I just looked up my own solution for this problem.

I didn’t have such issues, because my logic for generating array for change is completely different.

So you can either try to modify statObj.change at the end of your code

or you need to refactor the way you are building this array for change. To give you advices about this one - it will require thorough review of all your code. Not sure if I am able to do this right now.

Hey, thanks a lot for getting back. I read about the reduce method, but I am not sure how to make it work with this problem. Let me read more.

I am working with this problem for a week now and I feel I am finally close to passing all the tests. Once I do that I may refactor the code too. I know the code is tedious. I totally agree with you. I too have lost so many times lol. But this one made me reach farthest on the problem. And I really want to pass the tests now.

Thanks again so much :star2:

Keep in mind, reduce() is just how I would do that, it’s not necessary to use it.
You can always use some looping instead of reduce.

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