Checking Results With Number.isFinite()

Before sending the invoices, the bookkeeper prints each café’s price per kilo for October, then the month’s average, to check that every café was charged its listed price. This month’s list includes Corner Mill, which ordered nothing in October. Python refuses to divide by its zero kilos, but JavaScript answers and carries on.

The Python script calculates each price with float(cafe["amount"]) / cafe["kg"]. It prints three prices, then stops at Corner Mill with ZeroDivisionError: division by zero. The port converts cents to euros and grams to kilos, then formats each price with the invoices’ euro formatter:

Language: JavaScript
const euros = new Intl.NumberFormat("en-US", {
  style: "currency",
  currency: "EUR",
});

const cafeTotals = [
  { name: "Larder Lane", grams: 36000, cents: 66600 },
  { name: "Bluebell Café", grams: 9000, cents: 17280 },
  { name: "Northside Kitchen", grams: 27000, cents: 46440 },
  { name: "Corner Mill", grams: 0, cents: 0 },
];

for (const cafe of cafeTotals) {
  const price = cafe.cents / 100 / (cafe.grams / 1000);
  console.log(`${cafe.name}: ${euros.format(price)} per kg`);
}

The port runs to the end:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.