Exact Change

題目:
  給定3個參數,第1個為價格、第2個現金、第3個是收銀機裡面各種貨幣的數量,題目要我們準確地找零,如果無法找零則return "Insufficient Funds",而如果零錢剛剛好則return "Closed",其他則返回該找的各種貨幣面額以及金額

  function checkCashRegister(price, cash, cid) {
  var change;
  // Here is your change, ma'am.
  var USDColl = [100.00, 20.00, 10.00, 5.00, 1.00, 0.25, 0.10, 0.05, 0.01];
  var USD = {
    0.01 : "PENNY",
    0.05 : "NICKEL",
    0.10 : "DIME",
    0.25 : "QUARTER",
    1.00 : "ONE",
    5.00 : "FIVE",
    10.00 : "TEN",
    20.00 : "TWENTY", 
    100.00 : "ONE HUNDRED"
  };
  var total = 0;
  USDColl = USDColl.reverse();
  change = cash - price;
  var changeColl = [];
  cid.forEach(function(emt)
              {
                 total += emt[1];
                 changeColl.push(emt[1]);

              });

                   total  = total.toFixed(2);  

  if(total < change)
    return "Insufficient Funds";
  else if(total == change)
    return "Closed";
  else
  {
      var result = [];
      var amount = 0;
      var index = findLoc(change);
      for(var i=index;i >= 0;i--)
      {
          if( change > 0  )
          {
            if(changeColl[i] < change)
            {
                change -= changeColl[i];
                change = change.toFixed(2);
                result.push([USD[USDColl[i]], changeColl[i]]);
            }
            else
            {
                amount = Math.floor(change/USDColl[i]);
                change -= amount * USDColl[i];
                change = change.toFixed(2);
                result.push([USD[USDColl[i]], amount * USDColl[i]]);
            }
          }
      }  
      var distinct = [];
      result.forEach(function(emt)
                    {
                        if(emt[1] > 0)
                          distinct.push(emt);
                    });
      return (change == 0 ) ?  distinct : "Insufficient Funds";
  }

  function findLoc(num)
  {
      var index = -1;
       for(var i=0;i < USDColl.length;i++ )
       {
           if(USDColl[i] > num)
             index = i -1;
       }
      return index;
  }
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.10],
// ["QUARTER", 4.25],
// ["ONE", 90.00],
// ["FIVE", 55.00],
// ["TEN", 20.00],
// ["TWENTY", 60.00],
// ["ONE HUNDRED", 100.00]]

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

測試的正確結果

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]) 应该返回一个数组.
checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应该返回一个字符串.
checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应该返回一个字符串.
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]) 应该返回 [["QUARTER", 0.50]].
checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]) 应该返回 [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]].
checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应该返回 "Insufficient Funds".
checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应该返回 "Insufficient Funds".
checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) 应该返回 "Closed".

Solution:

  題目給了我們價格、付的現金和收銀機裡面的各種面額貨幣的金額,也暗示數量,所以一開始我先用array把美元所有的面額由大到小列出來,再來寫了一個Object裡面有面額與該面額的名稱(英文中我們用哪個詞形容那種面額的貨幣),然後一開始我先把收銀機裡面所有的貨幣依序取出來放進changeColl這個array中,再來將金額加總,並判斷如果金額小於要找的零錢數,則return "Insufficient Funds",如果金額相等則return return "Closed"。
  再來就是處理需要換找多種面額的狀況,做法是先依據要找的零錢的金額大小找出最大可以找給客人的面額貨幣(在USDColl),Ex:$96 就要先找出貨幣面額小於$96且大於比較小的面額的貨幣,也就是$20,因為比$100小且小於$96的貨幣面額中最大的是$20,然後因為該array是依小到大(USDColl = USDColl.reverse();)的順序排列,然後用for loop對能找的美元面額的array做訪問,然後在判斷收銀機中該面額的貨幣有沒有超過要找的零錢的總額,沒有則將要找的錢的總額減掉要收銀機裡面的該面額的金額,並將找的貨幣面額跟金額加入result array裡面,如果收銀機中該面額的貨幣超過要找的金額的總額,則用Math.floor來計算要找的貨幣面額的數量及總金額,再加入result的array裡面,然後因為是依照貨幣面額進行計算,所以一定會有某些面額的貨幣找零數量為0,此時就要將結果 array中貨幣面額的數量為0的過濾掉,並且return,要注意的是有種狀況是收銀機裡面的金額是大於要找零的金額,但是構成該金額的貨幣沒辦法找,因為他沒有剛好可以找零的零錢,Ex: 要找$0.5 而收銀機中只有$0.01和$1,這時雖然收銀機中的錢"夠找給客人"但因為貨幣沒辦法換成更小的零錢或面額,所以沒辦法將正確的金額找給客人,不然會虧錢。



**P.S. / Reference:  Global Object

          

results matching ""

    No results matching ""