计算找零

请编写一个用于收银机的函数 checkCashRegister():它的第一个参数为售价 price、第二个参数为支付金额 cash、第三个参数为收银机內的金额 cid。

cid 是包含货币面值的二维数组。

函数 checkCashRegister() 应返回含有 status 属性和 change 属性的对象。

如果收银机內的金额少于应找回的零钱数,或者你无法返回确切的数目时,返回 {status: "INSUFFICIENT_FUNDS", change: []}。

如果收银机內的金额恰好等于应找回的零钱数,返回 {status: "CLOSED", change: [...]},其中 change 的属性值就是收银机內的金额。

否则,返回 {status: "OPEN", change: [...]},其中 change 键值是应找回的零钱数,并将找零的面值由高到低排序。

货币单位Unit 面值
Penny 0.01美元(PENNY)
Nickel 0.05 美元(NICKEL)
Dime 0.1 美元(DIME)
Quarter 0.25 美元(QUARTER)
Dollar 1 美元(ONE)
Five Dollars 5 美元(五)
Ten Dollars 10 美元(TEN)
Twenty Dollars 20 美元(TWENTY)
One-hundred Dollars 100美元(ONE HUNDRED)

下面的抽屉里现金数组示例:

[
  ["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]]) 应返回一个对象。

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]]) 应返回 {status: "OPEN", change: [["QUARTER", 0.5]]}。

checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]) 应返回 {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}。

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应返回 {status: "INSUFFICIENT_FUNDS", change: []}。

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应返回 {status: "INSUFFICIENT_FUNDS", change: []}。

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应返回 {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}。

代码实现如下:

function checkCashRegister(price, cash, cid) {
  let change = [];

  let unit = {
    "ONE HUNDRED": 100,
    TWENTY: 20,
    TEN: 10,
    FIVE: 5,
    ONE: 1,
    QUARTER: 0.25,
    DIME: 0.1,
    NICKEL: 0.05,
    PENNY: 0.01,
  }

  let cidUnitCount = getUnitCount(cid, unit);
  let remain = cash - price, isSuit = true;
  for (let key in unit) {
    if (unit[key] <= remain) {
      const unitPrice = unit[key];
      const unitTotalCount = cidUnitCount[key];
      let curTotalPrice = 0, curCount = 0;
      while (curTotalPrice < remain && curCount < unitTotalCount) {
        curCount++;
        curTotalPrice = curCount * unitPrice;
      }
      if (curTotalPrice > remain) {
        curCount--;
        curTotalPrice = curCount * unitPrice;
      }
      if (curCount !== 0) {
        change.push([key,curTotalPrice ]);
        remain = (remain - curTotalPrice).toFixed(2);
      }
      if (curCount !== unitTotalCount) {
        isSuit = false;
      }
    }
  }
  let result;
  if (remain == 0) {
    if (isSuit) {
      result = { status: "CLOSED", change: cid };
    } else {
      result = { status: "OPEN", change: change };
    }
  } else {
    result = { status: "INSUFFICIENT_FUNDS", change: [] };
  }
  return result;
}

function getUnitCount(arr, unit) {
  let result = {};
  arr.forEach(item => {
    const [key, value] = item;
    result[key] = Math.round(value / unit[key]);
  })
  return 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]]);

checkCashRegister(3.26, 100, [["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", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])

你可能感兴趣的:(javascript)