String Indexing and Length, online
Six activities your students do on their own devices. See who's finished, and their scores.
String Indexing and Length in one minute
What students need to know
Since indexing starts at 0, index 1 is the second character. length is accessed directly as a property, not called as a method.
Strings are indexed starting at 0, just like arrays. length counts every character in the string, including spaces.
Mistakes students make
- Use square brackets [], not round brackets, to access a character.
- Missing the opening square bracket before the index.
- Missing the closing square bracket after the index.
- 'length' is misspelled as 'lenght'.
What students will learn in this module
Every question in Module 29
Quick questions
- Does word[0] access the first character of a string?AYesBNo
- Does .length give the number of characters in a string?AYesBNo
- Is () used instead of [] to access a character?AYesBNo
- Does .length need parentheses after it, like .length()?AYesBNo
- Is 'Length' with a capital L valid JavaScript syntax?ANoBYes
Key terms
Students type the meaning of:
length
Match the pairs
word[0]Match it to what it doesword.lengthMatch it to what it doesAccesses the second character of a stringMatch it to what it doesA property that never needs parenthesesMatch it to what it doesconsole.log(word(0));Match it to what it doesconsole.log(word.length());Match it to what it doesFill in the blanks
console.log(word_0]);
Hint: Type the symbol that opens an index access.
console.log(word.______);
Hint: Type the property that gives a string's number of characters.
let ch = word[_];
Hint: Type the index of the second character.
let name = "Amy"; console.log(name.______);
Hint: Type the property that gives a string's number of characters.
let size = name.______;
Hint: Type the property that gives a string's number of characters.
Spot the error
Question 1
Aconsole.log(word(0));Blet word = "cat";Cconsole.log(word.length);
Question 2
Alet text = "dog";Bconsole.log(text(1));Cconsole.log(text.length);
Question 3
Alet text = "cat";Bconsole.log(text.length);Cconsole.log(text0]);
Question 4
Alet word = "sun";Bconsole.log(word[0]);Cconsole.log(word.length);Dconsole.log(word[1);
Question 5
Alet name = "Amy";Bconsole.log(name[0]);Cconsole.log(name[1]);Dconsole.log(name[2]);Econsole.log(name.lenght);
