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
constarray 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
elifbecomeselse 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 declaresoutcomewithletabove theif. - 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 withreverse=True. The port compares two teams instead:b.points - a.pointsfor points, largest first. A tie gives0, which is falsy, so the||operator moves on to goal difference. - Named once,
byRank, the compare function is handed totoSorted()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 afor...ofloop. A counter from1stands in forenumerate().- The results go into a working copy. Python’s
copy.deepcopy()becomesstructuredClone(), 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.
beforeandafterhold each team’s place in the two tables, and the move is the difference. Anotherletandifturn it intoup,downorsame.- 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: