Submission link - https://bigfrontend.dev/problem/convert-snake_case-to-camelCase/discuss/916
/**
* @param {string} str
* @return {string}
*/
function snakeToCamel(str) {
let result = str[0]; // in any case we want to keep first char as it is
for(let i = 1; i < str.length; i++) { // begin from i=1 as we already have 0th index char
/** main logic:
1. current char `i` must be '_'
2. previous char must not be '_'
3. next char must not be '_'
4. current char must less than 2nd last of string
**/
if(str[i] == '_' && str[i-1] != '_' && str[i+1] != '_' && i < str.length - 1) {
result += str[i+1].toUpperCase();
i++; // increment because we already consider i+1 in previous line.
} else {
result += str[i]; // else just add in the result string
}
}
return result; // 🍻 return the camelCase because that's the way
}
In the last few weeks, I was not excited about solving problems and doing my side project. It was more like an unwanted break I took.
I tried this problem first with regex and couldn't succeed but when I look at a few solutions I realized that I was near to a solution.
It's just that I didn't work with regex before. I decided not to solve this problem with regex and better try to solve it with iteration.
- Create an empty output string and append the first char
- In both camel case and snake case, the first character is always a small case
- And it takes care of the case where the first character is
_
- We iterate over the input string and iteration begins at 1 because we already covered the first character above
- We want to make the eligible character (a letter after
_
) upper case only when- The current character is
_
- because we want to make its next letter upper case - If the previous/next character isn't
_
- If previous/next and current character is_
then it's a contiguous underscore and we don't want to touch it - The current character index must be 2nd last character in the input string because -
- In both camel case and snake case, the last character is always a small case
- And it takes care of the case where the last character is
_
- We increment the current count by 1 because we already consider the next character case
- The current character is
- Otherwise, we append the current character in the output string
- After for-loop, we return constructed string
- Create an empty string
result
and add the first character - We iterate over input string
str
with a for-loop and begin at 1- Because we already covered the first character in step 1
- We want to make the character after
_
to upper case if -- The current character
str[i]
is_
- The previous/next character is not
_
(because that will make it contiguous underscore) - The current count
i
is less than string length (2nd last character in thestr
) - If these conditions are met then we make
str[i+1]
to upper case and append to theresult
string - We increment
i
s value because we already coveredi+1
in the above step
- The current character
- Else we append the current character in the
result
string (because there is nothing to transform in else case)
🌻 ✨
I'm excited to improve the solution and code walkthrough. Feel free to drop a comment, or send a PR or send memes @knowkalpesh.