Javascript调试-函数对于特定输入未正确运行-FreeCodeCamp收银机项目

时间:2019-06-07 11:55:29

标签: javascript

我正在尝试为freeCodeCamp收银机项目创建JavaScript收银机功能。

其工作方式的说明如下:

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

cid是列出可用货币的2D数组。

抽屉中现金阵列示例:

[["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: [...]},并以现金入帐作为密钥更改的值(如果该值等于应付款)。

否则,返回{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

1 个答案:

答案 0 :(得分:1)

一些解决方法,但是乘以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