Rotating the Rota With push() and shift()
In this lesson, you ported the Monday script’s first step, which moves last week’s person from the front of the rota to the back, and met the four methods that add and remove items at the two ends of an array.
- The list carries over as an array literal, with a
const. Theconstfixes the name, not the array’s contents, so the rotation can still change the array. - Python’s
append()is JavaScript’s.push(), which adds an item to the end of the array. - Python’s
pop()can take a position, but JavaScript’s.pop()takes none: it always takes the last item, so the line-for-line port,rota.pop(0), takes Bea. Taking from the front has a method of its own,.shift(), which does what Python’spop(0)does. - Watch out for Python’s method names: an array has no
append()method, so callingrota.append(...)fails with “TypeError: rota.append is not a function”, and.push()is the name to write instead. - When the rotation line runs,
.shift()hands back the item it took off, as Python’spop(0)does..push()hands back the array’s new length, where Python’sappend()changes the list and hands backNone. .unshift()puts an item at the front, as Python’sinsert(0, x)does, and like.push()it hands back the new length, whereinsert()hands backNone.- Watch out at an empty array: there,
.pop()and.shift()hand backundefined, where Python’spop()raises anIndexError, and the rotation’s other method then adds thatundefinedto the rota: check the length before you rotate.
Here’s the rotation step of the Python script, monday_rota.py:
rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"]
rota.append(rota.pop(0))
print(f"On call this week: {rota[0]}")
print(rota)
It prints this:
On call this week: Dev
['Dev', 'Sam', 'Ines', 'Kofi', 'Bea', 'Marta']
In the port, monday-rota.js, .shift() takes Marta off the front, and .push() adds her at the
end:
const rota = ["Marta", "Dev", "Sam", "Ines", "Kofi", "Bea"];
rota.push(rota.shift());
console.log(`On call this week: ${rota[0]}`);
console.log(rota);
Run with node monday-rota.js, it prints what the Python step prints. Node’s console.log()
spaces the array’s brackets a little differently, but it’s the same six names in the same order:
On call this week: Dev
[ 'Dev', 'Sam', 'Ines', 'Kofi', 'Bea', 'Marta' ]
The .push() call in that line hands back 6, the six people on the rota, and the line ignores
it, because the change to the rota is what the script needs.
If the step runs twice by mistake, Sam is on call and Dev waits at the back. Running the move
backwards undoes one rotation: .pop() takes Dev off the back, and .unshift() puts him at the
front, where Python writes rota.insert(0, rota.pop()):
const rota = ["Sam", "Ines", "Kofi", "Bea", "Marta", "Dev"];
rota.unshift(rota.pop());
console.log(`On call this week: ${rota[0]}`);
console.log(rota);
Dev is on call again:
On call this week: Dev
[ 'Dev', 'Sam', 'Ines', 'Kofi', 'Bea', 'Marta' ]
At the back of an array, .push() adds and .pop() removes. At the front, .unshift() adds and
.shift() removes.
Next up: Replacing Tuples With Arrays, a text lesson where you’ll see how an array does the job of a tuple for the script’s out-of-hours window, since JavaScript has no tuples, and why porting the parentheses goes wrong.