So, I had to research how to properly use regular expression, but I made a functional JS Script! Took about 2 hours to get right. This was a small project challenge I found on free code camp, so I decided to to take it on. The project was to take a sentence and make it title case. Meaning "Hurry UP lOckLier" should be made into "Hurry Up Locklier". Here's the code I made:
function titleCase(str) { var splitStr = str.split(' '); var re = /^[a-z]/; var newStr = []; for(var i = 0; i < splitStr.length; i++){ var word = splitStr.toLowerCase(); var newChar = word.charAt(0).toUpperCase(); word = word.replace(re, newChar); newStr.push([word]); } newStr = newStr.join(' '); return newStr;}titleCase("HEY CHECK OuT THIS phRASE");It might be overly complicated, as I'm sure there's a shorter and easier way to do this. But as a newbie to JS this was quite an exciting experience to see that it worked exactly as intended.
function titleCase(str) { var splitStr = str.split(' '); var re = /^[a-z]/; var newStr = []; for(var i = 0; i < splitStr.length; i++){ var word = splitStr.toLowerCase(); var newChar = word.charAt(0).toUpperCase(); word = word.replace(re, newChar); newStr.push([word]); } newStr = newStr.join(' '); return newStr;}titleCase("HEY CHECK OuT THIS phRASE");It might be overly complicated, as I'm sure there's a shorter and easier way to do this. But as a newbie to JS this was quite an exciting experience to see that it worked exactly as intended.