JavaScript Arrays for Pythonistas (Summary)

Congratulations, you made it to the end of the course! The team lead’s Monday script now runs in JavaScript. monday-rota.js moves Marta to the back of the queue, takes Sam off for his leave, puts Lena in straight after Dev, and posts the same lines to the team channel as the Python script, starting with “On call this week: Dev”.

Every step of that script is an array operation, and none of them needs a loop. In this course, you learned how to:

  • Build the rota as an array and read it with rota[0], rota.at(-1), and .length, since rota[-1] quietly gives undefined
  • Add and remove at the ends with .push(), .pop(), .shift(), and .unshift(), so rota.push(rota.shift()) rotates the queue
  • Hold a pair like the out-of-hours window in an array, [18, 8], or in an object, { start: 18, end: 8 }, where Python uses a tuple
  • Take copies with .slice(), .toReversed(), and .concat(), from the next three weeks to the escalation list
  • Change an array in place with .splice(), which removes Sam with rota.splice(1, 1) and inserts Lena with rota.splice(1, 0, "Lena")
  • Search with .includes() and .indexOf(), and check for -1 before you splice
  • Sort a copy with .toSorted(), passing (a, b) => a - b for numbers or (a, b) => b - a for the busiest weeks first

Keep the Habits That Carry Over

Much of what you know about lists still holds. Positions start at zero, and .slice() follows Python’s slicing rules, stopping before the end position and counting negative positions from the back, though it takes no step. The default sort orders plain names as Python’s does, so the HR wiki gets “Bea, Dev, Ines, Kofi, Lena, Marta” in both languages.

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.