Mental Model
A string is a sequence of characters you can look at one by one from start to end.
Analogy: Imagine reading a sentence letter by letter to understand or find something specific.
"H" -> "e" -> "l" -> "l" -> "o" -> null ↑
"H" -> "e" -> "l" -> "l" -> "o" -> null ↑
"H" -> "e" -> "l" -> "l" -> "o" -> null ↑
"H" -> "e" -> "l" -> "l" -> "o" -> null
↑"H" -> "e" -> "l" -> "l" -> "o" -> null
↑"H" -> "e" -> "l" -> "l" -> "o" -> null
↑"H" -> "e" -> "l" -> "l" -> "o" -> null
↑"H" -> "e" -> "l" -> "l" -> "o" -> null All characters printed in order: H e l l o
class StringTraversal { constructor(str) { this.str = str; } printCharacters() { for (let i = 0; i < this.str.length; i++) { console.log(this.str[i]); // print current character } } } const example = new StringTraversal("Hello"); example.printCharacters();
for (let i = 0; i < this.str.length; i++) {console.log(this.str[i]); // print current characterfor (let i = 0; i < this.str.length; i++) {
for (let i = 0; i < this.str.length; i++) {
str[2] return when str = "hello"?text in JavaScript?const s = "world";
let result = "";
for (let i = 0; i < s.length; i++) {
if (i % 2 === 0) {
result += s[i];
}
}
console.log(result);word on a new line, but it prints an extra undefined. What is the problem?const word = "test";
for (let i = 0; i <= word.length; i++) {
console.log(word[i]);
}i < word.length instead of <= -> Option Di < length not <= to avoid out-of-bounds [OK]inputStr, which code snippet correctly reverses the string by traversing it from the end to the start?Pass the test and this pattern moves unknown