Question 11
Back to Exercise
The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?
const title = "learn java script fast";
const slug = title.replace(" ", "-");
console.log(slug);
- A replace cannot replace spaces
- B replace with a string pattern changes only the first match – use replaceAll(" ", "-") or replace(/ /g, "-") Correct
- C slug must be declared with let
- D The hyphen needs escaping
Correct Answer: Option B
replaceAll(" ", "-") (or a /\s+/g regex, which also collapses repeated spaces) gives "learn-java-script-fast".
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!