此算法的目的是返回更改的有组织数组(包含货币)。
我构建了一个嵌套的while循环来循环,只要"更改"不等于0然后while循环函数中的每个循环,而"更改"大于0(即:不是-ve)。
我错过了什么?
//
function checkCashRegister(price, cash, cid) {
var change = cash-price;
var chArr = [["PENNY", 0],
["NICKEL", 0],
["DIME", 0],
["QUARTER", 0],
["ONE", 0],
["FIVE", 0],
["TEN", 0],
["TWENTY", 0],
["ONE HUNDRED", 0]];
while (change !== 0) {
while (change - 100 > 0) {
chArr[8][1] += 100;
change -=100;
if (change <= 0) {break;}
}
while (change - 20 > 0) {
chArr[7][1] += 20;
change -=20;
if (change <= 0) {break;}
}
while (change - 10 > 0) {
chArr[6][1] += 10;
change -=10;
if (change <= 0) {break;}
}
while (change - 5 > 0) {
chArr[5][1] += 5;
change -=5;
if (change <= 0) {break;}
}
while (change - 1 > 0) {
chArr[4][1] += 1;
change -=1;
if (change <= 0) {break;}
}
while (change - 0.25 > 0) {
chArr[3][1] += 0.25;
change -=0.25;
if (change <= 0) {break;}
}
while (change - 0.1 > 0) {
chArr[2][1] += 0.1;
change -=0.1;
if (change <= 0) {break;}
}
while (change - 0.05 > 0) {
chArr[1][1] += 0.05;
change -=0.05;
if (change <= 0) {break;}
}
while (change - 0.01 > 0) {
chArr[0][1] += 0.01;
change -=0.01;
if (change <= 0) {break;}
}
if (change <= 0) {break;}
}
// Here is your change, ma'am.
return chArr;
}
checkCashRegister(17.46, 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]]);
答案 0 :(得分:3)
您只需检查单个现金部件,并在那里仅使用检查其余部分是否大于更改。在while循环中不需要破坏条件。
缺少的是检查更改是否足够。
function checkCashRegister(price, cash, cid) {
var change = cash-price;
var chArr = [["PENNY", 0], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]];
while (change >= 100 && cid[8][1] >= 100) {
chArr[8][1] += 100;
cid[8][1] -= 100;
change -=100;
}
while (change >= 20 && cid[7][1] >= 20) {
chArr[7][1] += 20;
cid[7][1] -= 20;
change -=20;
}
while (change >= 10 && cid[6][1] >= 10) {
chArr[6][1] += 10;
cid[6][1] -= 10;
change -=10;
}
while (change >= 5 && cid[5][1] >= 5) {
chArr[5][1] += 5;
cid[5][1] -= 5;
change -=5;
}
while (change >= 1 && cid[4][1] >= 1) {
chArr[4][1] += 1;
cid[4][1] -= 1;
change -=1;
}
while (change >= 0.25 && cid[3][1] >= 0.25) {
chArr[3][1] += 0.25;
cid[3][1] -= 0.25;
change -=0.25;
}
while (change >= 0.1 && cid[2][1] >= 0.1) {
chArr[2][1] += 0.1;
cid[2][1] -= 0.1;
change -=0.1;
}
while (change >= 0.05 && cid[1][1] >= 0.05) {
chArr[1][1] += 0.05;
cid[1][1] -= 0.05;
change -=0.05;
}
while (change > 0 && cid[0][1] >= 0.01) {
chArr[0][1] += 0.01;
cid[0][1] -= 0.01;
change -=0.01;
}
// Here is your change, ma'am.
return chArr;
}
var register1 = [["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]],
register2 = [["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]];
console.log(checkCashRegister(17.46, 20.00, register1));
console.log(register1);
console.log(checkCashRegister(19.50, 20.00, register2));
console.log(register2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
我认为您应该重新考虑您的代码:
function checkCashRegister(price, cash) {
var change = cash - price;
var chObj = {
PENNY: 0,
NICKEL: 0,
QUARTER: 0,
ONE: 0,
FIVE: 0,
TEN: 0,
TWENTY: 0,
ONE_HUNDRED: 0
}
// Performs an euclidian division to get the quotient
// (in 750$, there are 7 times 100 ie quotient = 7)
chObj.ONE_HUNDRED = Math.floor(change / 100);
// Now get the remainder (750 minus 7 times 100)
change = change % 100;
// So on...
chObj.TWENTY = Math.floor(change / 20);
change = change % 20;
chObj.TEN = Math.floor(change / 10);
change = change % 10;
chObj.FIVE = Math.floor(change / 5);
change = change % 5;
chObj.ONE = Math.floor(change);
change = Math.floor((change - Math.floor(change)) * 100);
chObj.QUARTER = Math.floor(change / 20) / 100;
change = change % 25;
chObj.NICKEL = Math.floor(change / 10) / 100;
change = change % 10;
chObj.NICKEL = Math.floor(change) / 100;
return chObj;
}
var chObj = checkCashRegister(245, 1000);
for (var key in chObj) {
if (chObj.hasOwnProperty(key)) {
console.log(key + " : \t" + chObj[key]);
}
}
结果(可通过控制台查看):
PENNY : 0
NICKEL : 0
QUARTER : 0
ONE : 0
FIVE : 1
TEN : 1
TWENTY : 2
ONE_HUNDRED : 7
我认为可以进一步改善。请参阅小提琴here。
答案 2 :(得分:0)
您的问题是,您正在检查等式为0,而您的实际值为0.00 .. [某事]您可以看到它是一个已知问题here
将最后一个if语句更改为if (change.toFixed(2) <= 0) {break;}
BTW我建议你把你所有的钱计算都改为美分,以避免这种问题(而不是1.0美元使用100美分)。
答案 3 :(得分:-1)
原因如下:每种类型的最终硬币都不会添加到阵列中。
说金额是0.02,2美分。然后在最后一个循环中,
while (change - 0.01 > 0) {
chArr[0][1] += 0.01;
change -=0.01;
if (change <= 0) {break;}
}
守卫(0.02 - 0.01 > 0
)为真,因此输入循环并且变为0.01。
下次,0.01 - 0.01 > 0
false ,因此未输入循环。
然后外循环一次又一次地运行,因为变化仍然是0.01并且它永远不会变低。
因此>
应该是>=
。然后可以删除所有断点以及整个外循环。