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!
Oldest comments (25)
Maybe not the best option :) but it works.
JS:
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
python
Haskell:
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
.A beginner solution
My solution in js
Haskell
Source-Code
Available on repl.it.
How about in Go?
Playground
This test was easy because i've written a python package that convert snake_case to camelCase and vice-versa :)
github.com/rafa-acioly/animal_case
Crystal
Tests
And benchmarked:
(Chrome 77, Win 10, 100K runs):
86 ms for LaBlatte
79 ms for SavagePixie
56 ms for Asfo
55 ms for BlessedTMahuni
54 ms for HVHarish
30 ms for Anders
(Firefox 69, Win 10, 100K runs):
116 ms for LaBlatte
55 ms for HVHarish
53 ms for Asfo
51 ms for BlessedTMahuni
29 ms for SavagePixie
23 ms for Anders
(Edge, Win 10, 100K runs):
208 ms for LaBlatte
127 ms for HVHarish
91 ms for Asfo
77 ms for BlessedTMahuni
46 ms for SavagePixie
27 ms for Anders
Javascript:
JS