Skip to content

Latest commit

 

History

History
24 lines (18 loc) · 706 Bytes

camelCase.md

File metadata and controls

24 lines (18 loc) · 706 Bytes
  • Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first letter capitalized without spaces.

For instance:

"hello case".camelCase() => HelloCase
"camel case word".camelCase() => CamelCaseWord

Solution:

String.prototype.camelCase = function () {
  return this.replace(/(?:^\w|[A-Z]|\b\w)/g, function (i) {
    return i.toUpperCase();
  }).replace(/\s/g, "");
};

console.log("test case".camelCase()); // "TestCase"
console.log("camel case method".camelCase()); // "CamelCaseMethod"
console.log("".camelCase()); // ""