Keeping Order Numbers Exact

With weights in grams and money in cents, the port’s totals match Python’s. Under each invoice line, though, the script lists the café’s orders by the web shop’s nineteen-digit order numbers, and the café will search for them exactly as printed.

Python reads the export with json.loads(), whose integers are exact at any size, and prints order 1849327348870116187: 12.6 kg first. JavaScript’s JSON.parse() does the job of json.loads(). JSON gets its own treatment later. Here’s the port, with the export trimmed to Larder Lane’s three orders in a template literal, which spans lines like a triple-quoted string:

Language: JavaScript
const EXPORT_TEXT = `{"total": "1303.20", "orders": [
    {"id": 1849327348870116187,
     "cafe": "Larder Lane", "kg": 12.6},
    {"id": 1851209668946987412,
     "cafe": "Larder Lane", "kg": 11.6},
    {"id": 1856614906249543762,
     "cafe": "Larder Lane", "kg": 11.8}
]}`;

const shopExport = JSON.parse(EXPORT_TEXT);
for (const order of shopExport.orders) {
  console.log(`  order ${order.id}: ${order.kg} kg`);
}

Predict the three lines. Will the IDs match?

Language: Program Output
  order 1849327348870116000: 12.6 kg
  order 1851209668946987500: 11.6 kg
  order 1856614906249543700: 11.8 kg

There’s no error, but every ID has lost its last digits. The café would search for order 1849327348870116000, which the shop never issued.

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.