Tell us what’s happening:
Describe your issue in detail here.
My code seems to work but I still can’t get through.
I don’t know what to write more.
Your code so far
function titleCase(str) {
let regex = /\b\w+('\w+)?\b/g;
let newString = "";
let textArray = str.match(regex);
let length = textArray.length - 1;
// for loop to create the new string where:
// - a word gets chosen from the textArray
// ==> with index starting at 0
// and
// ending with length
// - the chosen word gets lowerCased smallWorld
// - the first letter and the other part without
// the first letter gets into word
for (let i = 0; i <= length; i++) {
let smallWord = textArray[i].toLowerCase();
let smallWordLength = smallWord.length;
let firstLetter = smallWord.charAt(0).toUpperCase();
let otherHalf = smallWord.slice(1, smallWordLength);
let word = "";
if (otherHalf == undefined) {
word = firstLetter;
//console.log(word);
} else {
word = firstLetter + otherHalf;
//console.log(word);
}
newString = newString + " " + word;
//console.log(newString);
}
//console.log(newString);
return newString;
}
titleCase("I'm a little tea pot");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Title Case a Sentence
Link to the challenge: