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
This challenge comes from bestwebua on CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (25)
Replace
to the rescue!very clever :)
Doesn't match the first word, should be
/(?:^|\s)([a-z])/g
for that or something.I don't capitalise the first word of a camelCase construct. As far as I'm aware, it isn't customary in JavaScript.
But you are right, if one were to capitalise the first word as well, the regular expression would have to be modified accordingly.
The confusion is because the description says
camelCase
but they meanPascalCase
.Here comes a naive oneliner:
For every word, it uppercases its first letter and lowercases the following ones
Nice one liner :). But if s is an empty string, then str[0] will be undefined and toUpperCase will throw an error.
It's not about finding edge cases. This is a great solution for a coding challenge... Most coding challenges are things you'd put in production anyways
My solution in js
Maybe not the best option :) but it works.
JS:
How about in Go?
Playground
Crystal
Tests
JS
A beginner solution
Haskell
Source-Code
Available on repl.it.