Exercise 7: Remove All Whitespace

Problem Statement

Function `removeWhitespace(str)` banao jo string se sabhi whitespace characters remove kare. Example: removeWhitespace("hello world") => "helloworld"

Sample Output:

removeWhitespace("hello world") => "helloworld"
removeWhitespace("  a  b  c  ") => "abc"

Solution

const removeWhitespace = (str) => String(str ?? "").replace(/\s+/g, "");

Explanation

Overall Goal:

  • String se sabhi spaces remove karna.

Real world:

  • Data cleaning: remove spaces.
  • Formatting: compact strings.