javascript - 收银机免费代码营算法需要帮助
问题描述
我需要有关 FreeCodeCamp 上收银机挑战的代码方面的帮助。我正在通过除“OPEN”以外的其他检查
对于第二次检查,我得到了正确的细分 ["QUARTER":0.25], ["QUARTER":0.25] 即两个季度,但我不知道如何将它们加在一起并将其返回为 ["QUARTER":0.5] .
对于第三个,细分几乎就在那里,但它并没有带走最后一分钱,所以我在最终细分中缺少一个 [“PENNY”:0.01]。当我检查剩余的零钱时,它是一便士。
所以我真正需要帮助的是将变化作为每种类型单位的组合值返回,看看为什么它不返回第三种情况下的总金额。
function checkCashRegister(price, cash, cid) {
var cashAvailable = cid;
var units = [
["PENNY", 0.01],
["NICKEL", 0.05],
["DIME", 0.1],
["QUARTER", 0.25],
["ONE", 1],
["FIVE", 5],
["TEN", 10],
["TWENTY", 20],
["ONE HUNDRED", 100]
].reverse()
var cashAvailable = cid.slice().reverse()
var stat = {
status: '',
change: []
};
var changeRequired = cash - price;
var totalCash = cashAvailable.flat().filter(x => {
return isNaN(x) == false
}).reduce((a, b) => {
return a + b
}).toFixed(2)
var unitsNeeded = []
if (totalCash == changeRequired) {
stat.status = 'CLOSED'
stat.change = cid
} else if (totalCash < changeRequired) {
stat.status = 'INSUFFICIENT_FUNDS'
stat.change = []
} else if (totalCash > changeRequired) {
for (var i = 0; i < units.length; i++) {
while (changeRequired >= units[i][1] && cashAvailable[i][1] > 0) {
unitsNeeded.push(units[i])
cashAvailable[i][1] -= units[i][1]
console.log((changeRequired -= units[i][1]).toFixed(2))
}
if (changeRequired > units[8][1]) {
stat.status = 'INSUFFICIENT_FUNDS'
stat.change = []
} else {
stat.status = 'OPEN';
stat.change = unitsNeeded
}
}
}
return stat
}
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", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]
])
这是两个检查。
解决方案
下面的演示使用了一个类和 ES6 Map。详细信息在演示中评论。
/* This Map represents what the till has initially. Each value is
the count of each type -- not the monetary value.*/
let funds = new Map([
['One Hundred', 2],
['Twenty', 10],
['Ten', 20],
['Five', 20],
['One', 50],
['Quarter', 20],
['Dime', 25],
['Nickel', 30],
['Penny', 100]
]);
/** Register(funds)
@Params: funds[Map]: key = name / value = count not monetary
A class that has the following methods:
- set/get change
Allows direct access to this._change constructor property.
- transact(cost, payment)
Simply subtracts cost from payment to determine amount of
change.
- distribute(title = 'Change')
Takes a given amount and distributes it as currency in the
smallest amount of denominations. If a title isn't passed,
the default is used.
- remaining(map, title = 'Remaining in Till')
Pass the Map of change and return a Map of remaining money
from the fund Map. If a title isn't passed, the default is
used.
- displayMap(map)
A utility method to aid in displaying Maps.
*/
class Register {
constructor(funds) {
this.denomination = new Map([
["One Hundred", 100],
["Twenty", 20],
["Ten", 10],
["Five", 5],
["One", 1],
["Quarter", 0.25],
["Dime", 0.1],
["Nickel", 0.05],
["Penny", 0.01]
]);
this.distribution = funds;
this._change = 0.00;
}
set change(money) {
this._change = money;
}
get change() {
return this._change;
}
transact(cost, payment) {
this.change = payment - cost;
return this.change;
}
distribute(title = 'Change') {
let dist = new Map();
dist.set('||', `~~~~~~~~~~[${title}]~~~~~~~~~~`);
let total = Number(this.change);
let denom = Array.from(this.denomination.entries());
for (let [key, value] of denom) {
if (total >= value) {
let money = Math.floor(total / value);
dist.set(key, money);
total = total % value;
}
}
return dist;
}
remaining(map, title = 'Remaining in Till') {
for (let [key, value] of map.entries()) {
if (this.distribution.has(key)) {
let fund = this.distribution.get(key);
this.distribution.set(key, (fund - value));
}
}
map.set('|', `~~~~~~~~~~[${title}]~~~~~~~~~~`);
return this.distribution;
}
displayMap(map) {
for (let [key, value] of map.entries()) {
console.log(key + ': ' + value);
}
}
}
// Instantiate class instance as `r` pass the funds Map
const r = new Register(funds);
// Arbitrary values
let cost = 252.63;
let payment = 300.00;
// set/get this.change from constructor
r.transact(cost, payment);
// Returns the change in the least amount of denominations
const dist = r.distribute();
// Returns the remaining count of each denomination after change
const till = r.remaining(dist);
// Display Maps
r.displayMap(dist);
r.displayMap(till);
推荐阅读
- c# - Azure:通过证书从云工作者连接到密钥保管库
- rest - 简单的 CRUD 端点 Vs。更复杂的端点
- c++ - 如何使用右侧或左侧图像推断(推断)视差图中像素的坐标?
- xamarin - 在 Xamarin 中使用 Moq 进行 NUnit 单元测试
- python - 类错误中的递归函数:python 2.7 中的“全局名称 'XXX' 未定义”但在 python 3 中有效
- php - DRUPAL 7:从 Ajax 调用触发下载请求
- mysql - 使此查询更快(按整数列排序)
- xamarin - 如何在滚动视图中延迟加载图像
- sql - LIKE 运算符、N 和 % SQL Server 不适用于 nvarchar 列
- c# - WPF,使用触发器设置背景颜色