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 League Table Script

Porting the League Table Script

You’ve ported Nadia’s script one part at a time. In this project, most of it goes into one file for the booking site.

A port that runs can still publish the wrong table, with the wrong confirmed leaders. So each line that copies, freezes or sorts is written the JavaScript way, and the port should print Python’s first eight lines, and the what-if line is left for the exercise.

  • The points for a win, a draw and a loss were a tuple, so they become a frozen array, Object.freeze([3, 1, 0]), frozen where it’s created, never a list that another name can still change.
  • The confirmed table moves across as a const array of objects. Python’s loop unpacks each result tuple into names. JavaScript’s way comes later, so each match is a record with named fields. Freezing goes one level deep, so a loop freezes each record too, to match Python’s tuple of tuples, which can’t change.
  • record() changes the team it’s given, as in Python.
  • Each elif becomes else if. The outcome is set in a branch and read after it, and unlike a Python name, a name declared in a JavaScript block ends at its brace. So the port declares outcome with let above the if.
  • The result is pushed onto the form, and .shift() drops the oldest, where Python pops the first item.
  • Python’s standings() sorts by a tuple key with reverse=True. The port compares two teams instead: b.points - a.points for points, largest first. A tie gives 0, which is falsy, so the || operator moves on to goal difference.
  • Named once, byRank, the compare function is handed to toSorted() by name. On this table, a key function, (t) => -t.points, put Wanderers on top, and sorting upward and then calling .toReversed() would turn Dynamo and Wanderers round.
  • places() maps each team’s name to its place. With no dict comprehension in JavaScript, the port fills an empty object in a for...of loop. A counter from 1 stands in for enumerate().
  • The results go into a working copy. Python’s copy.deepcopy() becomes structuredClone(), a global with nothing to import, which copies every level. A spread copy would share the records, and the confirmed leaders would come out as Rovers. The loop reads each match’s named fields and records the result.
  • After the loop, Rovers still have 13 points in the confirmed table, and the form is as it was. The clone’s Rovers took the win, with 16. So the move column compares two separate tables, as Python’s does.
  • before and after hold each team’s place in the two tables, and the move is the difference. Another let and if turn it into up, down or same.
  • Python calls join() on the separator, but JavaScript calls it on the array, with the separator inside. Each f-string switches to a template literal, down to the confirmed leaders.

Here’s the port, league-table.js, with the what-if line still to come:

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 You’ve ported Nadia’s script one part at a time. Now most of it goes into one file for the booking site. A port that runs can still publish the wrong table, with the wrong confirmed leaders.

00:13 So each line that copies, freezes or sorts is written the JavaScript way, and the port should print Python’s first eight lines, and the what-if line is left for the exercise.

00:26 The points for a win, a draw and a loss were a tuple, so they become a frozen array, frozen where it’s created, never a list another name still changes. The confirmed table moves across as a const array of objects.

00:42 Python’s loop unpacks each result tuple into names. JavaScript’s way comes later, so each match is a record with named fields. Freezing goes one level deep, so a loop freezes each record too, to match Python’s tuple of tuples, which can’t change.

01:01 record() changes the team it’s given, as in Python. Each elif becomes else if. The outcome is set in a branch and read after it, and unlike a Python name, a name declared in a JavaScript block ends at its brace.

01:17 So the port declares it with let above the if. The result is pushed onto the form, and .shift() drops the oldest, where Python pops the first item.

01:30 Python’s standings() sorts by a tuple key with reverse=True. The port compares two teams instead: b - a for points, largest first.

01:41 A tie gives zero, which is falsy, so the || operator moves on to goal difference. Named once, byRank, the compare function is handed to toSorted() by name.

01:53 Here, a key function put Wanderers on top. Sorting upward and then reversing would turn Dynamo and Wanderers round.

02:04 places() maps each team’s name to its place. With no dict comprehension in JavaScript, the port fills an empty object in a for...of loop.

02:14 A counter from one stands in for enumerate().

02:19 The results go into a working copy. Python’s copy.deepcopy() becomes structuredClone(), a global with nothing to import, which copies every level.

02:29 A spread copy would share the records, and the confirmed leaders would come out as Rovers.

02:35 The loop reads each match’s named fields and records the result. Look at Rovers in the confirmed table. After the loop, how many points do Rovers have in that table?

02:49 Your turn: pause the video and predict.

02:57 Still thirteen, and the form is as it was. The clone’s Rovers took the win, with sixteen. So the move column compares two separate tables, as Python’s does.

03:11 before and after hold each team’s place in the two tables, and the move is the difference. Another let and if turn it into up, down or same.

03:23 Python calls join() on the separator, but JavaScript calls it on the array. Each f-string switches to a template literal, down to the confirmed leaders.

03:35 node --check reads the file and says nothing: no syntax errors. Run it, and JavaScript prints eight lines. They match Python’s first eight, line for line.

03:49 Python’s key stops at goal difference, so the port does too. Both sorts are stable, so Dynamo and Wanderers keep the table’s alphabetical order. In your own rankings, end with a tie-breaker that can’t tie, such as the team name.

04:05 The rule: port what each line means, then check that the output matches. Freeze fixed data where you create it, and keep the direction inside the compare function.

04:18 Next up: the exercise Finish the League Table. You’ll add the last line: what the postponed Albion and United match would do if Albion won 1-0 at home. The provisional table must stay as it is, and JavaScript then prints Python’s nine lines.

Become a Member to join the conversation.