Loading video player…

Reading Characters With [] and at()

Reading Characters With [] and at()

In this lesson, you ported the helper’s full-stop check and read single characters the JavaScript way. The review bot rejects a commit subject that ends with a full stop, and PAY-2304’s title, Retry failed card payments., has one.

  • Python’s len() function becomes the length property in JavaScript, read after a dot with no parentheses: title.length is 27. Watch out: len() doesn’t exist in JavaScript, and title.length() is a TypeError, because a property isn’t a function.
  • Brackets read a position counted from the start, as in Python: title[0] is R, and title[26], one less than the length, is the full stop.
  • JavaScript’s brackets don’t count from the end: they look up a position by its number, and the string has no position -1. The line-by-line port of the check, const last = title[-1];, gives undefined, JavaScript’s value for nothing there, so the check prints Ends with a full stop: false where Python prints True, with no error.
  • Reading a position that isn’t there gives undefined in JavaScript, with no error. A position past the end gives undefined too, where Python raises an IndexError, so check the length when the text might be short.
  • at() takes a position, as the brackets do, and it also accepts a negative one. A negative position counts back from the end, as in a Python index, so title.at(-1) is the full stop. Like the brackets, at() gives undefined, not an error, when the position isn’t there.
  • at() takes positions from the start too, but JavaScript code keeps the brackets for those and reaches for at() for a negative position.
  • As in Python, a string can’t change in place. In the helper’s port, title[0] = "r" stops with a TypeError.
  • For text like these titles, each position holds one character. Text with emoji is where JavaScript’s positions and Python’s part ways, a later topic.

Here’s the check with the fix in place:

Language: JavaScript
const title = "Retry failed card payments.";

const last = title.at(-1);
console.log(`Ends with a full stop: ${last === "."}`);

When you run it with node ticket-branch.js, it gives the Python script’s answer, spelled the JavaScript way:

Language: Program Output
Ends with a full stop: true

Next up is the text lesson Slicing Text With slice(). There, you’ll cut the commit subject to 50 characters and read the ticket number with the slice() method, where your Python intuition carries over.

00:00 In this lesson, you’ll port one check from the branch-name helper and read single characters the JavaScript way. The review bot rejects any commit subject that ends with a full stop.

00:13 So the helper checks the last character of the ticket’s title. In Python, title[-1] reads it, counting back from the end. PAY-2304’s title ends with a full stop, so the script’s answer is True for this ticket.

00:31 The title line carries over with const in front. Before the last character, take two reads you already know from Python. Python’s len() is a function you call on the text.

00:44 JavaScript keeps the size on the string itself, as its length property, read after a dot with no parentheses. title.length counts the same 27 characters.

00:57 Watch out for the Python habit here: len() doesn’t exist in JavaScript, and length() with parentheses is a TypeError, because a property isn’t a function.

01:08 Brackets read a position counted from the start, as in Python, so position 0 is the capital R.

01:16 The check’s title[-1] ports as it is, brackets and all. The print line becomes console.log() with a template literal, with $ in front of the hole as in the commit message you wrote, and === takes the place of == in the comparison.

01:33 Python said yes, this title ends with a full stop. What will JavaScript print on the last line? Your turn: pause the video and predict.

01:48 JavaScript answers false, with no error and no warning. The check misses the full stop that’s right there in the title. Python’s negative positions count back from the end, so -1 is the full stop.

02:06 JavaScript’s brackets only start from the front: 0 is the R, and 26, one less than the length, is the full stop. Brackets look up a position by its number, and -1 isn’t one this string has.

02:21 So title[-1] gives undefined, JavaScript’s value for nothing there.

02:28 Reading a position that isn’t there doesn’t stop JavaScript with an error. The undefined travels on, into a comparison with a full stop that comes out false without a word.

02:39 The same goes for a position past the end, where Python raises an IndexError, so check the length when the text might be short.

02:50 For a negative position, JavaScript has the at() method. The fix swaps the brackets for a call to it. Like the brackets, at() takes a position, but a negative one counts back from the end, as Python’s brackets do, so -1 is the full stop.

03:07 And like the brackets, it gives undefined, not an error, when the position isn’t there. Run the port again, and the last line prints true, matching Python apart from JavaScript’s lowercase spelling.

03:23 at() reads positions from the start too. But JavaScript code keeps the brackets for those, and reaches for at() with a negative position, which counts back from the end.

03:35 Brackets count only from the start, at() also takes a negative position that counts back from the end, and the length is a property. As in Python, a string can’t change in place: in the helper’s port, assigning a new first letter stops with a TypeError.

03:53 For text like these titles, each position holds one character; text with emoji is where JavaScript’s positions and Python’s part ways, a later topic.

04:03 Next up is Slicing Text With slice(), a text lesson. There, you’ll cut the commit subject to fifty characters and read the ticket number with the slice() method, where your Python intuition carries over.

Become a Member to join the conversation.