Я некоторое время застрял в этом проекте Freecodecamp, когда покупка сделана его возврата, точное изменение, необходимое для сортировки счетов от самой высокой конфессии до самой низкой, но когда я снова покупаю во второй раз, когда изменение возвращается, но на этот раз он сортирует счета из самой низкой деноминации до самого высокого и CID (денежный дрозд), кажется, не исходит. Вот мой код < /p>
const calculateChange = (price, cash, cid) => {
/* calculates the total amount in the cid array */
let totalCid = Number(cid.reduce((sum, el) => sum + el[1], 0).toFixed(2));
/* calculate change to give to customer when purchase is made */
let change = Number((cash - price).toFixed(2));
if (totalCid < change) {
changeDue.innerText = "Status: INSUFFICIENT_FUNDS";
} else if (totalCid === change && price < cash) {
changeDue.innerText = "Status: CLOSED " + cid;
} else {
let changeArr = [];
/* reverse cid array so it starts giving change from the highest denomination */
cid.reverse().forEach((el) => {
let coinName = el[0];
let totalAmountInDrawer = Number(el[1]);
let equivalentOfAmountInDrawer = Number(currencyUnit[coinName]);
let availableAmount = Number((totalAmountInDrawer / equivalentOfAmountInDrawer).toFixed(2));
let amountCollected = 0;
while (change >= equivalentOfAmountInDrawer && availableAmount > 0) {
change = Number((change - equivalentOfAmountInDrawer).toFixed(2));
/* tried to decrement the totalAmountInDrawer but to no avail */
totalAmountInDrawer--;
availableAmount--;
amountCollected++;
}
if (amountCollected > 0) {
const amount = Number((amountCollected * equivalentOfAmountInDrawer).toFixed(2));
changeArr.push([` ${coinName}: ` + `$${amount}` ]);
return changeArr;
}
});
changeDue.innerText = "Status: OPEN " + changeArr;
}
}
< /code>
Я попытался уменьшить TotalAmountRawer, но массив CID не закончится. Я не понимаю, что делаю неправильно
Подробнее здесь: https://stackoverflow.com/questions/796 ... has-been-m