Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Porting the Night-Bus Report

Porting the Night-Bus Report

In this project, you ported the night-bus report’s first three sections from Python to JavaScript, and left the supervisors for the exercise.

Python JavaScript
"route": "N3", False route: "N3", false
for trip in trips: for (const trip of trips)
late.append(line) late.push(line)
enumerate(late, start=1) late.entries(), printing index + 1
dict.fromkeys(routes, 0) a short loop that starts each route at 0
totals.items() Object.entries(totals), destructured in the header
for...else search .find(), then if...else on undefined
  • Every loop header declares its variable. Leave the const out, and the module stops with ReferenceError: trip is not defined, because ES (ECMAScript) modules run in strict mode.
  • Route names keep their order in an object. Keys written as plain whole numbers, such as hours, come out sorted, and a Map keeps them in order.
  • Ported with } else { after the loop, the file stops with SyntaxError: Unexpected token 'else'. JavaScript checks the whole file before it runs any of it, so even the sections above the loop don’t print.

Here are the report’s sections in JavaScript, as you left them. They read last night’s trips, which the full file at the end of this page lists:

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

00:00 In this project, you’ll port the whole night-bus report to JavaScript, one section at a time, with the loops you’ve learned. Here’s the Python script the team runs every morning.

00:11 At the top are last night’s trips: eleven departures on three routes. Then the routes, busiest first by riders, and the supervisor on duty for each. Below them, the report has four sections, each a loop you’ve already ported on its own.

00:28 Run it, and Python prints the morning report. You’ll rebuild the first three sections here, and pair up the supervisors yourself in the exercise that follows.

00:42 In the JavaScript file, the data ports the way you’ve seen. Property names lose their quotes, and Python’s False becomes lowercase false. The two lists become const arrays.

00:57 The first section collects the late trips. Port its loop at speed, and it’s tempting to keep the header the way Python wrote it. Node stops on that line: ReferenceError, trip is not defined.

01:12 Python’s loop creates trip for you. JavaScript’s doesn’t, and a module runs in strict mode, so the loop variable needs its const. Inside the loop, append becomes push, and the f-strings become template literals.

01:28 Then Python’s enumerate numbers the lines. JavaScript asks the array for its entries, and adds one where a person reads the number. Run it, and Node prints the same five late departures as Python.

01:44 The next section totals the delays per route.

01:50 Python starts every route at zero with fromkeys.

01:54 A JavaScript object has no fromkeys, so a short loop sets each route to zero.

02:02 The loop that adds each trip’s delay ports line for line. To print the pairs, Python’s items becomes Object.entries, and the header destructures each pair into a route and its total.

02:17 Node prints the three totals in Python’s order. Route names like N3 aren’t number-like, so the object keeps them in the order they went in. Hours as keys would come out sorted, and that’s a job for a Map.

02:33 The third section looks for the first canceled trip on the busiest route, the first in the list.

02:41 Python’s loop ends in an else, for when no trip matches.

02:47 Port it line by line, else and all. Node rejects the whole file: SyntaxError, unexpected token else. Node checks the whole file before it runs any of it, so not even the first two sections print.

03:03 JavaScript loops have no else. This loop only searches, so ask the array instead. Find takes an arrow function that checks the route and the canceled flag, joined with a double ampersand.

03:19 When nothing matches, find returns undefined, so an ordinary if and else takes over the loop’s else. Node prints the report’s first three sections, down to the trip canceled at ten past midnight.

03:35 One section is left. Python’s zip pairs each route with its supervisor, and it stops at the end of the shorter list. JavaScript has no zip, so you’ll write the pairing yourself, and choose where it stops.

03:50 So here’s the report the JavaScript way. Every loop declares its variable.

03:58 Entries gives you positions, and Object.entries gives you an object’s pairs. And a search that Python ends with else becomes find, with a check for undefined.

04:10 Next up is the exercise, Finish the Night-Bus Report. There, you’ll pair the routes with their supervisors and run the whole report in Node.

Become a Member to join the conversation.