JavaScript string exercises
JavaScript strings rarely throw errors. They just produce the wrong text: "5" + 1 is "51", replace() stops after one match, and ${name} in single quotes prints as-is. Each exercise here is a short function with one of those bugs.
Run the tests, read what came back, fix the line. Everything runs in a sandboxed worker in your browser.
The strings bugs you will meet
- replace() with a string pattern only replaces the first match; use replaceAll().
- ${} only works inside backticks.
- + with a string concatenates: "5" + 1 is "51".
- slice(0) returns the whole string, not everything after the first character.
- toFixed() returns a string, so adding to it concatenates.
The exercises
Medium
- String utility crashes the browserInfinite loops
- Array mapping returns empty objectsAsync and race conditions
- Data groups overwriting each otherLogic
- Array contains pending operationsAsync and race conditions
- Counter stuck at zeroScope and closure
- Parser getting stuck on mismatchesInfinite loops
- Number conversion rounding incorrectlyType
- Formatter appending extra charactersOff-by-one
- Formatter leaving unwanted charactersMutation and copying
- Mapping strings to numbersType
Errors you might be searching for
More JavaScript practice by topic
Questions
Do I need Node.js?
No. Your code runs in a sandboxed Web Worker in the browser.
Which JavaScript version?
Plain modern JavaScript, with no frameworks. The same bugs turn up in React and Node code.