我需要你的帮助来理解我在for循环中做错了什么。
我正在尝试编写一个函数,它接受账单的总金额和支付该账单的总现金。返回一个新对象,该对象描述收银员要退回的零钱总数。省略我不应该归还的任何类型的改变,例如,如果我不归还一张20美元的钞票,我就不把它包括在结果中。
我确实得到了带有结果的对象,但不是按预期的方式分割美元值。
我期望得到这样的结果:
{ tenDollar: 1, twoDollar: 1, oneDollar: 1, quarter: 3, penny: 2 }
但是得到这个:
{ tenDollar: 1, twoDollar: 0, oneDollar: 3, quarter: 0, dime: 7, penny: 7}
代码实现:
const calculateChange = function(total, cash) {
// Your code here
let arr = [];
let change = cash - total;
//console.log(change);
//creates an aaray of split numbers
var i = 1;
while (change > 0) {
arr.unshift((change % 10) * i);
change = Math.floor(change / 10);
i *= 10
}
let changeType = {tenDollar: 0, twoDollar: 0, oneDollar: 0, quarter: 0, dime: 0, penny: 0};
for(i = 0; i < arr.length; i++) {
if (arr[i] % 1000 === 0) {
changeType.tenDollar = arr[i] / 1000;
} else if (arr[i] % 200 === 0 && arr[i] >=200 ) {
changeType.twoDollar = arr[i] / 200;
} else if (arr[i] % 100 === 0) {
changeType.oneDollar = arr[i] / 100;
} else if (arr[i] % 25 === 0) {
changeType.quarter = arr[i] / 25;
} else if (arr[i] % 10 === 0) {
changeType.dime = arr[i] / 10;
} else if (arr[i] % 1 === 0) {
changeType.penny = arr[i] / 1;
}
}
return changeType;
};
console.log(calculateChange(2623, 4000));
我将这样做:
const moneys =
[ { face: 'fiftyDollar', val: 50 *100 }
, { face: 'twentyDollar', val: 20 *100 }
, { face: 'tenDollar', val: 10 *100 }
, { face: 'twoDollar', val: 2 *100 }
, { face: 'oneDollar', val: 1 *100 }
, { face: 'quarter', val: .25 *100 }
, { face: 'dime', val: .1 *100 }
, { face: 'penny', val: .01 *100 }
]
const calculateChange = (total, cash) =>
{
let result = {}
, change = cash - total
;
moneys.forEach( ({face,val}) =>
{
let count = Math.floor(change / val)
change %= val
if (count > 0) result[face] = count
})
return result
}
console.log( JSON.stringify( calculateChange( 2623, 4000 )) )
.as-console-wrapper { max-height: 100% !important; top: 0; }
你的代码逻辑是扭曲的,因为你试图匹配2个不同的套件。第一个序列arr
是(1000,100,10,1)
的10次幂,但由于$ 20
和quarters ($ 0.25)
的不同,其展开的次数不同。在你的测试(arr = [1000,300,70,7]
)中,70c
不能返回预期的3 quarters
,因为在下一个除法中7c
中的5c
会丢失。
我们对你的逻辑所能做的最好的代码是(但它永远不会给你期望的结果):
const calculateChange = function(total, cash)
{
let arr = []
, change = cash - total
;
// console.log(`${cash} - ${total} = ${change}`);
//creates an aaray of split numbers
var i = 1;
while (change > 0)
{
arr.push((change % 10) * i);
change = Math.floor(change / 10);
// console.log( i, JSON.stringify(arr), change, (i*10))
i *= 10
}
// console.log( 'arr =', JSON.stringify(arr), ' = ',arr.reduce((a,c)=>a+c,0))
let changeType = {};
for(i=arr.length; i--;)
{
let tenP = 10**i
if (tenP === 1000 && arr[i] >= 1000 ) { changeType.tenDollar = arr[i] / 1000; }
if (tenP === 100 && arr[i] >= 200 ) { changeType.twoDollar = Math.floor(arr[i] / 200); arr[i] %= 200; }
if (tenP === 100 && arr[i] >= 100 ) { changeType.oneDollar = arr[i] / 100; }
if (tenP === 10 && arr[i] >= 25 ) { changeType.quarter = Math.floor(arr[i] / 25); arr[i] %= 25; }
if (tenP === 10 && arr[i] >= 10 ) { changeType.dime = arr[i] / 10; }
if (tenP === 1 && arr[i] > 0 ) { changeType.penny = arr[i]; }
}
return changeType;
}
console.log( JSON.stringify( calculateChange(2623, 4000)))
.as-console-wrapper { max-height: 100% !important; top: 0; }
我认为你的方法不正确,
这是解决类似
的真实方法。(function () {
var app = angular.module("calcApp", []);
app.controller("calcController", function () {
this.amountChange = function (amount, money) {
//if(form.$invalid)
//return alert("Please check form and enter correct values");
var change = money - amount;
var remaining = change * 100;
var calcQuarter = Math.floor(remaining / 25);
remaining = remaining % 25;
var calcDime = Math.floor(remaining / 10);
remaining = remaining % 10;
var calcNickel = Math.floor(remaining / 5);
remaining = remaining % 5;
var calcPenny = Math.floor(remaining / 1);
alert(
"nThe total was $" +
amount +
" with " +
"$" +
money +
" tendered.nnPlease return $" +
change.toFixed(2) +
": " +
calcQuarter +
" quarters, " +
calcDime +
" dimes, " +
calcNickel +
" nickels, and " +
calcPenny +
" pennies, Thank you!"
);
};
});
})();
可以在这里找到完整的代码