Change only the first cat to dog.
Acceptance criteria
- 1.#out should show dog cat cat.
JavaScript Tutorial · JavaScript Strings
These 10 tasks go with the JavaScript String replace chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “replace swaps one piece of text for another and returns a new string.” .
1.Replace the first matchexercise · easy · 10 min
Change only the first cat to dog.
Passes when: #out should show dog cat cat.
2.Replace every matchexercise · easy · 10 min · practice plan
Change all three cats to dogs.
Passes when: #out should show dog dog dog.
3.Remove charactersexercise · easy · 10 min · practice plan
Strip the dashes out of the phone number.
Passes when: #out should show 07123456.
4.Swap a wordexercise · easy · 10 min · practice plan
Replace the word world with everyone.
Passes when: #out should show Hello everyone.
5.Clean up spacingexercise · medium · 10 min · practice plan
Trim the text and collapse the triple space.
Passes when: #out should show Ada Lovelace.
6.Fix only the first replacedbug fix · easy · 8 min
Every match should change, not just the first.
Passes when: #out should show dog dog dog.
7.Fix the result thrown awaybug fix · easy · 8 min · practice plan
replace returns a new string and leaves the original alone.
Passes when: #out should show goodbye.
8.Fix the case mismatchbug fix · easy · 8 min · practice plan
The capitalised word does not match the lowercase search.
Passes when: #out should show dog.
9.Fix the replacement left inbug fix · easy · 8 min · practice plan
The dashes should be removed, not turned into spaces.
Passes when: #out should show 07123456.
10.Fix the arguments swappedbug fix · easy · 8 min · practice plan
The text to find comes first and the replacement second.
Passes when: #out should show Hello everyone.
Between them these tasks cover replace, replaceAll and Removing text.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.