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

Taking Someone Off by Name

Taking Someone Off by Name

In this lesson, you ported how the Monday script searches the rota by name, and found out what JavaScript does when a name isn’t there.

  • Python asks with the in operator: "Kofi" in rota becomes rota.includes("Kofi"), a method on the array. Both answer yes for Kofi.
  • Watch out: in JavaScript, "Kofi" in rota is false and 3 in rota is true, because the in operator asks about keys, such as an array’s positions, not its values.
  • Python’s rota.index("Kofi") becomes rota.indexOf("Kofi"). Both return 3, counting from zero.
  • For a name that isn’t there, Python’s index() raises ValueError: list.index(x): x not in list. JavaScript’s .indexOf() doesn’t raise: it returns -1, a value that means “not found”.
  • Python’s count() has no array method to match.
  • JavaScript arrays have no remove() method. The port builds one from two steps: .indexOf() finds the first match, and .splice() takes one item out from there.
  • For a name that isn’t on the rota, .indexOf() returns -1, so the call becomes rota.splice(-1, 1). In Python, -1 is a position too, the last item. In JavaScript, square brackets don’t count from the end, but methods like .slice() and .splice() do. So it removes Marta, with no error.
  • Python’s remove() can’t do this: its search raises first. In JavaScript, “not found” is a value, so check it before you splice.
  • For arrays, .includes() and .indexOf() compare the way === does: the very same array, like Python’s is operator. Python’s in operator checks for equal contents instead. So an array of hours pairs won’t find a fresh [18, 8]: compare its parts, which the quiz practices.

Rui moved to the daytime team last month, and this week’s leave request is for him. Here’s the team lead’s Python:

Language: Python
rota = ["Dev", "Lena", "Ines", "Kofi", "Bea", "Marta"]
on_leave = "Rui"

rota.remove(on_leave)
print(rota)

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:02 Here’s this Monday’s rota, with Sam’s leave handled, Lena straight after Dev, and Marta last in the queue. In this lesson, you’ll port the Python that searches it by name, and see what each language does when a name isn’t there.

00:17 The script answers Kofi, who asked in chat if he’s on the rota. It also takes out anyone on leave, and this week’s request is for Rui, who moved to the daytime team last month.

00:28 For Rui, Python and JavaScript disagree, and only one of them tells you.

00:35 Kofi’s question first. Python asks with the in operator. JavaScript asks with .includes(), a method on the array, and both answer yes.

00:48 Watch out for JavaScript’s in operator, though: "Kofi" in rota is false, and 3 in rota is true, because it asks about keys, such as an array’s positions, not its values.

01:01 Next, where’s Kofi in the queue? Python’s index() method finds his position. As on strings, JavaScript’s .indexOf() finds him: both return 3, counting from zero.

01:15 Now look for Rui. Python’s index() raises a ValueError, because Rui isn’t in the list. JavaScript’s .indexOf() doesn’t raise.

01:26 As on strings, it answers -1: not found. Python’s count() has no array method to match. You’ll meet a way to count later.

01:39 The team lead’s Python script takes the name on leave out with remove(), which searches for it and deletes it. Run it with Rui, and Python stops with a ValueError: Rui isn’t in the list.

01:54 That error is a safety net. The team lead sees it and sorts out the request by hand.

02:01 The port keeps the name as a const and logs the rota at the end. But JavaScript arrays have no remove() method, so it builds one from two steps.

02:11 .indexOf() finds the first match, and .splice() takes one item out from there. It’s the line you’d write for any name. So what does the rota hold after it runs for Rui?

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

02:31 Marta’s gone. The output lists five names, and no error. The wrong person is off the rota, and nothing tells you.

02:44 Read the line from the inside out. .indexOf() answers -1 for Rui, so the call becomes .splice() from -1.

02:53 In Python, -1 is a position too: the last item. In JavaScript, square brackets don’t count from the end, but methods like .slice() and .splice() do.

03:06 So .splice() counts back from the end and removes Marta.

03:10 Python’s remove() can’t do this: its search raises first. In JavaScript, not found is a value, and .splice() takes it as a position.

03:22 In Python, you’d guard remove() with an if on_leave in rota: check. When the name’s there, remove() searches again.

03:31 In JavaScript, one search does both jobs: .indexOf() tells you whether Rui is there and where. The fix stores it in a const, and splices only when it isn’t -1.

03:45 Otherwise, both print a message. Run them with Rui, and the message prints, and all six names stay.

03:54 Here’s the rule. Not finding a value never raises in JavaScript. .includes() answers yes or no, and .indexOf() answers a position or -1, so not found is a value you check.

04:10 For arrays, both compare the way === does: the very same array, like Python’s is operator. Python’s in operator checks for equal contents instead.

04:22 So an array of hours pairs won’t find a fresh [18, 8]: compare its parts, which the quiz practices. Next up: Sorting the Rota for the Wiki, a text lesson.

04:35 You’ll sort the rota alphabetically for the HR wiki, and keep its real order as it is.

Become a Member to join the conversation.