DEV Community

loading...
Cover image for Solving 1 random codewars challenge in Typescript per day - Day 1

Solving 1 random codewars challenge in Typescript per day - Day 1

ivaanrl profile image Iván Roldán Lusich ・1 min read

Today's challenge (direct link):

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:

camelCase("hello case"); // => "HelloCase"
camelCase("camel case word"); // => "CamelCaseWord"
Enter fullscreen mode Exit fullscreen mode

My Solution:

export function camelCase(str: string): string {
  const splitStr = str.split(' ');

  return splitStr.map(word => word.charAt(0).toUpperCase() + word.slice(1)).join('');
}
Enter fullscreen mode Exit fullscreen mode

Well, this was a really simple one. Just split the original string at every space and then map over that array capitalizing every word.

Hopefully, we'll get a harder one next time. Have a nice day!

Discussion (0)

pic
Editor guide