JavaScript change calculator

\ $ \begingroup\ $ first thing that stands out is that your function is 140 line long, which is just besides much. You have put the comments in, which is good, but it would be simpler if you extracted that patch of code into a function .
This for loop does three things : Filters the cash for return, calculates the sum in register and copies the register. You can split it in three different functions .

  for (let i = 0; i < cid.length; i++) {
    // Filter for cash that can be used as return
    if (cid[i][1] < cash) {
      newArr.push(cid[i])
    }
    totalCash += cid[i][1]
    newArr0.push(cid[i])
  }

What you end up with is this :

  function calculateTotalInRegister(cashRegister) {
      let total = 0
      for (let i = 0; i < cashRegister.length; i++) {
          total += cashRegister[i][1]
      }

     return total
  }

  function cashUsedToReturn(cid) {
    const forReturn = []
    for (let i = 0; i < cid.length; i++) {
      if (cid[i][1] < cash) {
        forReturn.push(cid[i])
      }
    }

    return forReturn
  }

  function copyRegister(cid) {
    const copy = []
    for (let i = 0; i < cid.length; i++) {
      copy.push(cid[i])
    }

    return copy
  }

...

  const newArr0 = copyRegister(cid);
  const newArr = cashUsedToReturn(cid);
  var inCash = calculateTotalInRegister(cid);

then you have these checks :

  if (inCash === dif) {
    newObj.status = "CLOSED";
    newObj.change = newArr0;
  } else if (inCash < price) {
    newObj.status = "INSUFFICIENT_FUNDS";
  }

What is wyrd about this is that you pass in the register and return the status of the file and change, but not the new country of the register. besides, I 'm pretty sure that second condition is a wiretap. It should be :

  else if (inCash < dif) {
    newObj.status = "INSUFFICIENT_FUNDS";
  }

anyhow, you again seem to be doing two things : getting the newfangled register condition and calculating the change. So let 's split this into two more functions .

  function getStatus(inCash, dif) {
    if (inCash === dif) {
      return "CLOSED";
    } else if (inCash < dif) {
      return "INSUFFICIENT_FUNDS";
    } else {
      return "OPEN"
    }
  }

  function calculateChange(inCash, dif, newArr, newArr0) {
    ...
  }

  ...

  newObj.status = getStatus(inCash, dif)

This leaves us with the original function that looks like this :

function checkCashRegister(price, cash, cid) {
  const monetary = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
  const newObj = {"status": [], "change": []}
  const newArr2 = [];
  const newArr1 = [];
  const newArr0 = copyRegister(cid);
  const newArr = cashUsedToReturn(cid);
  var inCash = calculateTotalInRegister(cid);
  var dif = cash - price;
  let result = parseFloat(dif)
  let newValue = 0;
  let count = 0;

  newObj.status = getStatus(inCash, dif)
  newObj.change = calculateChange(inCash, dif, newArr, newArr0)

  console.log(newObj)
  return newObj;
}

Before looking into calculateChange, it is about time that we rename the variables. Monetary is kind of fine, the other ones are not .
Parameter criminal investigation command should be cashRegister.

newObj should be named solution with register.status and change properties like so :

    const result = {
        register: {
            status: getStatus(inCash, dif)
        },
        change: calculateChange(inCash, dif, newArr, newArr0)
    }

newArr1, newArr2, consequence, newValue and count can be left out, newArr0 - > cashRegisterCopy, newArr - > cashForReturn, inCash - > totalInRegister, dif - > amountToReturn .
The master serve is immediately barely this :

function checkCashRegister(price, cash, cashRegister) {
    const monetary = [["PENNY", 0.01], ["NICKEL", 0.05], ["DIME", 0.1], ["QUARTER", 0.25], ["ONE", 1], ["FIVE", 5], ["TEN", 10], ["TWENTY", 20], ["ONE HUNDRED", 100]];
    const cashRegisterCopy = copyRegister(cashRegister);
    const cashForReturn = cashUsedToReturn(cashRegister);
    var totalInRegister = calculateTotalInRegister(cashRegister);
    var amountToReturn = cash - price;

    const result = {
        register: {
            status: getStatus(totalInRegister, amountToReturn)
        },
        change: calculateChange(totalInRegister, amountToReturn, cashForReturn, cashRegisterCopy)
    }

    console.log(result)

    return result;
}

It turns out that cashRegisterCopy is unnecessary. You could equitable pass the cash register directly. Which brings me to the independent write out with the means you coded this - alternatively of passing cash register everywhere, you could create a CashRegister class .
Another thing is the manipulation of charming strings everywhere ( PENNY, NICKEL, and so forth ). Turn them into constants .
I would besides use a different data structure, just so the regulate does n't matter.

One more lean when working on monetary apps : do not use floats, but use integers. If you want to display the amount you can just divide the sum by 100 .
If you do the refactoring to the course, the code will look something like this :

class CashRegister {
  constructor(money) { this.money = money }

  // status of the register
  get status() { ... }

  // total amount of money in register
  get total() { ... }

  // returns the change and subtracts from this.money
  returnChange(price, cashGiven) { ... }
}

const cashRegiser = new CashRegister({
    PENNY: 101,
    NICKEL 205,
    DIME: 310,
    QUARTER: 425,
    ONE: 9000,
    FIVE: 5500,
    TEN: 2000,
    TWENTY: 6000,
    HUNDRED: 10000
})

const change = cashRegister.returnChange(3.26, 100)
const status = cashRegister.status

As for the algorithm for returning change, you can watch some videos on change make problem in javascript on youtube. possibly alternatively of passing the sum of money you have for each mint, you can pass the count of coins/bills you have in the cross-file and work with that : )

source : https://chickgolden.com
Category : Finance

Post navigation

Leave a Comment

Trả lời

Email của bạn sẽ không được hiển thị công khai.