gpt4 book ai didi

Javascript 调试 - 针对特定输入的功能无法正常运行 - freeCodeCamp 收银机项目

转载 作者:行者123 更新时间:2023-11-29 22:56:54 25 4
gpt4 key购买 nike

我正在尝试为 freeCodeCamp 收银机项目创建一个 javascript 收银机函数。

它应该如何工作的描述如下:

设计收银抽屉功能checkCashRegister()接受购买价格作为第一个参数 (price),付款作为第二个参数 (cash),现金抽屉 (cid) 作为第三个参数。

cid是一个列出可用货币的二维数组。

现金抽屉数组示例:

[["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]]

checkCashRegister()函数应始终返回带有状态键和更改键的对象。

返回{status: "INSUFFICIENT_FUNDS", change: []}如果现金抽屉里的零钱少于应付的零钱,或者如果您无法退回准确的零钱。

返回{status: "CLOSED", change: [...]}现金抽屉作为 key 找零的值(value),如果它等于应付的找零。

否则返回{status: "OPEN", change: [...]} , 以硬币和纸币的应有零钱,从高到低排序,作为找零键的值。

我的代码通过了 freeCodeCamp 的所有测试,只有一个除外。 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]]} .但它正在返回 {change: [], status: "INSUFFICIENT_FUNDS"} .

代码:

function checkCashRegister(price, cash, cid) {
var change = {};
var changeDue = cash - price;
var cidTotal = 0;
for (let i = 0; i < cid.length; i++) {
cidTotal += cid[i][1];
}
if (changeDue == cidTotal) {
change.status = "CLOSED";
change.change = cid;
return change;
}
change.change = [];
var denominations = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
var remaining = changeDue;
var units = ["PENNY", 'NICKEL', 'DIME', 'QUARTER', 'ONE', 'FIVE', 'TEN', 'TWENTY', 'ONE HUNDRED'];
for (let i = cid.length - 1; i >= 0; i--) {
let leastCount = denominations[i];
let vid = cid[i][1]; //value in drawer for current denomination
let n = 0;
while (remaining >= leastCount && vid > 0) {
vid -= leastCount;
remaining -= leastCount;
n ++;
}
if (n > 0) {
change.change.push([units[i], n * leastCount]);
}
}
console.log(change.change); //Console.log statement 1
console.log(remaining); //Console.log statement 2
if (remaining == 0) {
change.status = "OPEN";
} else {
change.status = "INSUFFICIENT_FUNDS"
change.change = [];
}
return change;
}


console.log(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]]));

我的代码通过了 freeCodeCamp 的所有测试,只有一个除外。 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]]}但它正在返回 {change: [], status: "INSUFFICIENT_FUNDS"} .

添加console.log(change.change)for之后循环显示 [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.03]]而不是预期的结果 [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]] .

添加console.log(remaining)for之后循环显示 0.009999999999994869而不仅仅是 0 .

最佳答案

一些解决方法,但是乘以 100 以计算整数怎么样?如果您可以准确地表示所有价格,那么这个小修改对我有用 ;-)如果您满意,投票按钮下方有一个接受按钮。

function checkCashRegister(price, cash, cid) {
var change = {};
var changeDue = (cash - price) * 100;
var cidTotal = 0;
for (let i = 0; i < cid.length; i++) {
cidTotal += cid[i][1] * 100;
}
if (changeDue == cidTotal) {
change.status = "CLOSED";
change.change = cid;
return change;
}
change.change = [];
var denominations = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
var remaining = changeDue;
var units = ["PENNY", 'NICKEL', 'DIME', 'QUARTER', 'ONE', 'FIVE', 'TEN', 'TWENTY', 'ONE HUNDRED'];
for (let i = cid.length - 1; i >= 0; i--) {
let leastCount = denominations[i] * 100;
let vid = cid[i][1] * 100; //value in drawer for current denomination
let n = 0;
while (remaining >= leastCount && vid > 0) {
vid -= leastCount;
remaining -= leastCount;
n ++;
}
if (n > 0) {
change.change.push([units[i], n * leastCount / 100]);
}
}
console.log(change.change); //Console.log statement 1
console.log(remaining); //Console.log statement 2
if (remaining == 0) {
change.status = "OPEN";
} else {
change.status = "INSUFFICIENT_FUNDS"
change.change = [];
}
return change;
}


console.log(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]]));

愚蠢的总和测试看起来也很有希望:

var nums = [];
var sum1 = 0, sum2 = 0, sum3 = 0;
for(var i=0;i<2556;i++) {
sum1 += i;
nums.push(i/100);
sum2 += nums[i];
sum3 += nums[i]*100;
}
console.log(sum1, sum2, sum3, nums);
//3265290 32652.899999999997 3265290

关于Javascript 调试 - 针对特定输入的功能无法正常运行 - freeCodeCamp 收银机项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56493844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com