Recursion with text
JavaScript · Unit 11: Recursion
- Full plan
- JavaScript
- 10 exercises
Strings work just like arrays: the first letter is t[0] and the rest is t.slice(1), a string with one letter fewer. The base case is usually the empty string, "".
const t = "sun";
console.log(t[0]);
console.log(t.slice(1));Prints
s un
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. Predict the output
This function counts the vowels. What does it print?
3. Complete the code
Fill in the blank so it prints "rats".
4. Find the case that fails
flip(t)should return the string backwards. With the empty string it should return an empty string. Which call breaks it?5. Predict the output
Now each call prints the string it gets. What does it print?
6. Find the bug
This program should print
true, because "wow" is a palindrome. Which line has the error?7. Complete the code
Fill in the comparison so it prints
true.8. Multiple choice
palindrome("Anna")givesfalse, with this lesson's function. Why?9. Put the lines in order
To check phrases you need to remove their spaces. Put
noSpacesin order: it returns the string without them.10. Predict the output
A whole phrase. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.