LearnFrenzy
Sign in Take a free test

String Methods · Find the Error · Question 11

Bug: only one space is replaced Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links