Exercise 18: Convert CamelCase to kebab-case

Problem Statement

Function `camelToKebab(str)` banao jo camelCase string ko kebab-case me convert kare. Example: camelToKebab("helloWorld") => "hello-world"

Sample Output:

camelToKebab("helloWorld") => "hello-world"
camelToKebab("myVariableName") => "my-variable-name"

Solution

const camelToKebab = (str) => {
  return String(str ?? "").replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`);
};

Explanation

Overall Goal:

  • CamelCase ko kebab-case me convert karna.

Real world:

  • CSS classes: naming conversion.
  • URL slugs: format conversion.