Skip to content

Latest commit

 

History

History
71 lines (58 loc) · 3.4 KB

File metadata and controls

71 lines (58 loc) · 3.4 KB

Solution

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
}

Short story

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.

Algorithm

  1. Create an empty output string and append the first char
    1. In both camel case and snake case, the first character is always a small case
    2. And it takes care of the case where the first character is _
  2. We iterate over the input string and iteration begins at 1 because we already covered the first character above
  3. We want to make the eligible character (a letter after _) upper case only when
    1. The current character is _ - because we want to make its next letter upper case
    2. 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
    3. The current character index must be 2nd last character in the input string because -
      1. In both camel case and snake case, the last character is always a small case
      2. And it takes care of the case where the last character is _
    4. We increment the current count by 1 because we already consider the next character case
  4. Otherwise, we append the current character in the output string
  5. After for-loop, we return constructed string

Code overview

  1. Create an empty string result and add the first character
  2. We iterate over input string str with a for-loop and begin at 1
    1. Because we already covered the first character in step 1
  3. We want to make the character after _ to upper case if -
    1. The current character str[i] is _
    2. The previous/next character is not _ (because that will make it contiguous underscore)
    3. The current count i is less than string length (2nd last character in the str)
    4. If these conditions are met then we make str[i+1] to upper case and append to the result string
    5. We increment is value because we already covered i+1 in the above step
  4. 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.