Looping over text
JavaScript · Unit 6: Strings
- Full plan
- JavaScript
- 10 exercises
A for...of goes through a string letter by letter, just like it goes through an array.
Spaces and punctuation get their own round too.
for (const letter of "sun") {
console.log(letter);
}Prints
s u n
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Complete the code
Complete it so it prints "ccaatt".
3. Complete the code
Complete the function that counts the vowels in a string.
4. Predict the output
What does this code print?
5. Find the case that fails
countA(text)should count how manyathe text has, uppercase or lowercase. Which call breaks it?6. Predict the output
What does this code print?
7. Find the bug
This program should remove the spaces and print "helloworld". Which line has the error?
8. Multiple choice
What does this function do?
9. Put the lines in order
Order the function that separates the letters with a space and turns them uppercase ("hello" gives "H E L L O").
10. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.