Last active
January 7, 2020 13:04
-
-
Save sryze/f67687f045c4aa806cf9b59495aa57b6 to your computer and use it in GitHub Desktop.
Capitalize every word in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function capitalizeWords(text) { | |
let result = ''; | |
let wordCharIndex = 0; | |
for (let i = 0; i < text.length; i++) { | |
let c = text.charAt(i); | |
if (/\s/.test(c)) { | |
wordCharIndex = 0; | |
} else { | |
if (wordCharIndex == 0) { | |
c = c.toUpperCase(); | |
} else { | |
c = c.toLowerCase(); | |
} | |
wordCharIndex++; | |
} | |
result += c; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This below line will throw error as it is a constant variable. Please change it to 'let'
Change from
Change To