Camel to Snake - FreeCodeCamp Daily Challenge
Camel to Snake
Although the title sounds like a circus act, today’s challenge is to convert strings written in camelCase to snake_case.
Camel Case is a style where words are joined without spaces and each word (except the first) starts with a capital letter, for example: camelCaseExample. Snake Case uses only lowercase letters and separates words with underscores, for example: snake_case_example.
Problem Statement
Given a camelCase string, return its snake_case version following these rules:
- The string contains only letters (A-Z and a-z) and always starts with a lowercase letter.
- Each uppercase letter marks the start of a new word.
- Convert all letters to lowercase.
- Separate words with underscores (
_).
Solution Approach
The first intuition is to iterate through the string and detect uppercase letters. Each time we find one, we convert it to lowercase and precede it with an underscore in the result. To identify uppercase letters in JavaScript, we can compare char === char.toUpperCase() or use char >= 'A' && char <= 'Z'.
Another option is to use regular expressions with replace, searching for all uppercase letters and replacing them with an underscore followed by the lowercase letter. The replace method allows passing a function to transform the match.
Implementation
Loop Solution
function toSnakeLoop(camelCaseStr) {
let snakeCaseStr = ''
for (let char of camelCaseStr) {
if (char === char.toUpperCase()) {
snakeCaseStr += `_${char.toLowerCase()}`
}
else {
snakeCaseStr += char
}
}
return snakeCaseStr
}Regular Expression Solution
function toSnakeRegex(camelCaseStr) {
return camelCaseStr.replace(/([A-Z])/g, (match) => {
return `_${match.toLowerCase()}`
})
}Complexity Analysis
- Time: , where is the length of the string. Both solutions traverse the string once.
- Space: , since a new string proportional to the input size is generated.
Final Thoughts
Both solutions are efficient and meet the requirements. The choice between loop or regular expression depends on personal preference and context. Regular expressions are usually more concise, while the loop may be clearer for those unfamiliar with regex.