// The checkout below runs fine and still refuses a correct cart. Fix totalCents.
const cart = [
{ name: "USB-C cable", price: 3.99 },
{ name: "Mouse", price: 19.99 },
{ name: "Keyboard", price: 45.50 },
];
// The payment API only accepts a whole number of cents.
function totalCents(items) {
const dollars = items.reduce((sum, item) => sum + item.price, 0);
return dollars * 100;
}
// Never charge a card unless the cart still totals what the customer was quoted.
function matchesQuote(items, quotedCents) {
return totalCents(items) === quotedCents;
}
console.log("Receipt: $" + (totalCents(cart) / 100).toFixed(2));
console.log("Charging:", totalCents(cart), "cents");
console.log("Customer was quoted 6948 cents. Match?", matchesQuote(cart, 6948));