Write simple .camelCase method (camel_case function in PHP, CamelCase in C# or camelCase in Java) for strings. All words must have their first lett...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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
Haskell:
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
C
Still very much learning - had to iterate an initial time to decide how large my result array should be once spaces are removed. Not sure if there's a better way. Still, this was fun - decided to iterate through the argument string in two different ways, to practice.
I agree with SavagePixie.
replace
is the way to go if you're in JS!Here's an extended version that covers all the cases I can think of...
Regex:
test in Debuggex
My solution in Swift with an extension of
String
:Isn't this camelCase and this is PascalCase?
python
Javascript: