FCC算法題--Exact Change
阿新 • • 發佈:2017-07-24
global 記得 可用 one web 設計 ons switch org
題目:
設計一個收銀程序 checkCashRegister()
,其把購買價格(price
)作為第一個參數 , 付款金額 (cash
)作為第二個參數, 和收銀機中零錢 (cid
) 作為第三個參數.
cid
是一個二維數組,存著當前可用的找零.
當收銀機中的錢不夠找零時返回字符串 "Insufficient Funds"
. 如果正好則返回字符串 "Closed"
.
否則, 返回應找回的零錢列表,且由大到小存在二維數組中.
當你遇到困難的時候,記得查看錯誤提示、閱讀文檔、搜索、提問。
這是一些對你有幫助的資源:
- Global Object
1 function checkCashRegister(price, cash, cid) {2 var change; 3 var sumCid; 4 5 // Here is your change, ma‘am. 6 var obj = { 7 penny: cid[0][1], 8 nickel: cid[1][1], 9 dime: cid[2][1], 10 quarter: cid[3][1], 11 one: cid[4][1], 12 five: cid[5][1], 13 ten: cid[6][1],14 twenty: cid[7][1], 15 oneHundred:cid[8][1] 16 }; 17 18 //購買價格(price), 付款金額 (cash), 收銀機中零錢 (cid) 19 change = cash - price; 20 //console.log("typeof(change):", typeof(change)); //number 21 //console.log("change:", change); 22 23 sumCid = 24 obj.penny + 25 obj.nickel + 26obj.dime + 27 obj.quarter + 28 obj.one + 29 obj.five + 30 obj.ten + 31 obj.twenty + 32 obj.oneHundred; 33 34 sumCid = Number(sumCid.toFixed(2)); //保留兩位小數 35 36 //console.log(typeof(sumCid)); //number 37 //console.log("sumCid:", sumCid); 38 39 if (sumCid < change) { 40 //console.log("sumCid小於change!"); 41 return "Insufficient Funds"; //余額不足 42 } 43 else if (sumCid === change) { 44 //console.log("sumCid等於change!"); 45 return "Closed"; 46 } 47 else { 48 // 1.零錢不夠,返回余額不足 49 // 2.零錢足夠,找回零錢 50 //console.log("sumCid大於change!"); 51 overChange(change, cid, sumCid); 52 } 53 } 54 55 function overChange(change, arr, sumCid) { 56 var array = []; 57 var len = arr.length; 58 59 for (var i = len - 1; i >= 0; i--) { 60 if (arr[i][1] === 0) continue; 61 62 if (arr[i][1] < change) { 63 array.push(arr[i]); 64 change -= arr[i][1]; 65 change = Number(change.toFixed(2)); 66 //console.log("current change:", change); 67 } 68 69 else if (arr[i][1] > change) { 70 var x = arr[i][0]; // 取得零錢名稱 71 //console.log("x:", x); 72 var y = 0; 73 74 switch(x) { 75 case "ONE HUNDRED": 76 y = Math.floor(change / 100) * 100; 77 break; 78 case "TWENTY": 79 y = Math.floor(change / 20) * 20; 80 break; 81 case "TEN": 82 y = Math.floor(change / 10) * 10; 83 break; 84 case "FIVE": 85 y = Math.floor(change / 5) * 5; 86 break; 87 case "ONE": 88 y = Math.floor(change / 1); 89 break; 90 case "QUARTER": 91 y = Math.floor(change / 0.25) * 0.25; 92 break; 93 case "DIME": 94 y = Math.floor(change / 0.10) * 0.10; 95 break; 96 case "NICKEL": 97 y = Math.floor(change / 0.05) * 0.05; 98 break; 99 case "PENNY": 100 y = Math.floor(change / 0.01) * 0.01; 101 break; 102 } 103 104 console.log(y); 105 console.log(sumCid - arr[i][1] < change); 106 107 if (y === 0 && sumCid - arr[i][1] < change) { 108 //console.log("Insufficient Funds"); 109 return "Insufficient Funds"; 110 } 111 112 else if (y === 0) { 113 continue; 114 } 115 116 else { 117 if (y < 1) { 118 y = y; 119 } 120 var array1 = []; 121 array1.push(arr[i][0]); 122 array1.push(y); 123 array.push(array1); 124 change -= y; 125 change = Number(change.toFixed(2)); 126 //console.log("current change:", change); 127 } 128 } 129 } 130 131 if (change === 0) { 132 //console.log("array:", array); 133 return array; 134 } 135 else { 136 //console.log("Insufficient Funds"); 137 return "Insufficient Funds"; 138 } 139 140 } 141 142 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]]);
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".
---------------------------------------------------------------------------------------
我的Free Code Camp地址
FCC算法題--Exact Change