Lesson 2 · Unit 03
Counting characters
Letters are numbers: count them in 26 slots
What you practise
- Character codes, a 26-slot count array, comparing counts
Try a question from this lesson
Pick one
Every character is stored as a number, and the letters a to z get 26 consecutive numbers. If "a" is 97, what is "d"?
Correct answer
100
Why
a, b, c, d are 97, 98, 99, 100. Because the codes are consecutive, a letter's code minus the code of "a" is its position in the alphabet: 0 for a, 3 for d.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Characters are numbers
Each character has a numeric code. The lowercase letters are consecutive, starting at 97 for "a".
So code - 97 maps a to z onto 0 to 25: a ready-made array index.
print(ord("a")) # 97
print(ord("d") - 97) # 3
print(chr(97 + 3)) # dconsole.log("a".charCodeAt(0)); // 97
console.log("d".charCodeAt(0) - 97); // 3
console.log(String.fromCharCode(100));console.log("a".charCodeAt(0)); // 97
console.log("d".charCodeAt(0) - 97); // 3
console.log(String.fromCharCode(100));Python noteord turns a one-character string into its code and chr turns a code back into a character.
JavaScript notecharCodeAt(0) reads the code of the first character. String.fromCharCode goes the other way.
TypeScript notecharCodeAt(0) returns a number: it gives NaN, not undefined, when the string is empty.
9 more exercises in the app
- 2× type the answer
- 1× order the lines
- 3× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.