Basic Algorithm Scripting - Title Case a Sentence

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:

Hello Vagynarys

Javascript offers a lot of useful methods that can simplify and make the code leaner

The solution requires to return the string by using the function that will take any strings and modify it based on the code within the function:

function titleCase(str) {
  return str.replace(/\b\w[\w']*/g, function(match) {
    return match.charAt(0).toUpperCase() + match.slice(1).toLowerCase();
  });
}
  1. The str.replace method is used with a regular expression pattern (/\b\w[\w']*/g) to match each word in the string.
  • \b represents a word boundary, ensuring that the match occurs at the beginning of each word.
  • \w matches any alphanumeric character (letters and digits).
  • [\w']* matches zero or more alphanumeric characters or apostrophes (to handle cases like “don’t”).
  • /g is a flag that enables global matching, which ensures that all words in the string are matched, not just the first one.
  1. The replace method replaces each matched word with the result of a callback function. The callback function takes the matched word (match) as an argument.
  2. Inside the callback function, the first character of match is capitalized using the charAt(0).toUpperCase() method chain. This ensures that the first letter of each word is capitalized.
  3. The slice(1).toLowerCase() method chain is used to extract the remaining characters of match (starting from the second character) and convert them to lowercase. This ensures that all other letters in each word are lowercase.
  4. The capitalized first letter and the lowercase remaining characters are then combined and returned as the replacement for the matched word.
  5. The replace method continues replacing all occurrences of words that match the regular expression pattern until all words have been processed.
  6. Finally, the modified string with capitalized words is returned as the result of the titleCase function.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.