JavaScript Numbers for Pythonistas (Summary)

Congratulations, you made it to the end of the course! The roastery’s batch planner now runs in JavaScript. For tonight’s paste of "37.5", batch-plan.js prints the same four lines as batch_plan.py, with the same beans and the same bill.

In the last exercise, you ran it on the three orders the head roaster compares: "37.5", "", and "1_200". You made the JavaScript stop on the blank paste as Python’s float() does and plan the 1,200 kg order, so both scripts agree on all three.

In this course, you learned how to:

  • Use Number for the planner’s ordinary whole and decimal values, and ask Number.isInteger() whether the stored value is whole
  • Guard a division that Python would have stopped, because dividing by zero gives NaN, Infinity, or -Infinity with no error
  • Use Math.floor(a / b) for the planner’s // calculation and Math.trunc() for int() on finite numbers, and where values can go below zero, pair Math.floor() with ((a % n) + n) % n and Math.trunc() with %
  • Round with Math, using Math.ceil() for the bags and Math.round(x * 100) / 100 for the cost to the cent
  • Convert a paste with Number(), after treating blank text as missing, and check it with Number.isNaN()
  • Read a number off the front of text such as "17.5kg" with parseFloat()
  • Format numbers last with .toFixed() and Intl.NumberFormat, with an explicit locale, for separators, euros, and percentages

Keep the Habits That Carry Over

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.