JavaScript String replace — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript String replace lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Exercises(5)
- 1
Replace the first match
easy10 minChange only the first cat to dog.
- 2
Replace every match
easy10 minChange all three cats to dogs.
- 3
Remove characters
easy10 minStrip the dashes out of the phone number.
- 4
Swap a word
easy10 minReplace the word world with everyone.
- 5
Clean up spacing
medium10 minTrim the text and collapse the triple space.
Find the bug(5)
- 1
Fix only the first replaced
easy8 minEvery match should change, not just the first.
- 2
Fix the result thrown away
easy8 minreplace returns a new string and leaves the original alone.
- 3
Fix the case mismatch
easy8 minThe capitalised word does not match the lowercase search.
- 4
Fix the replacement left in
easy8 minThe dashes should be removed, not turned into spaces.
- 5
Fix the arguments swapped
easy8 minThe text to find comes first and the replacement second.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace