In changing the case of a word or sentence to uppercase or lowercase, we can employ the toUpperCase() or toLowerCase() javascript's built-in method respectively. How about Capitalizing a word or sentence? There is no built-in method yet that I know of in Javascript. Do you know of any?
This episode of Algorithm 101 shows us 8 Ways to Capitalize a Given Sentence.
sentenceCapitalization('njoku'); // "Njoku"
sentenceCapitalization('njoku samson ebere'); // "Njoku Samson Ebere"
Prerequisite
To tag along comfortably, you need to have basic understanding of javascript's string and array methods.
Let's Capitalize a Sentence Using:
- .forEach(), toLowerCase(), split(), slice(), toUpperCase(), push() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        sentence
          .toLowerCase()
          .split(" ")
          .forEach(element => {
            let fistElement = element[0].toUpperCase();
            let splicedElement = element.slice(1);
            capitalizedSentence.push(fistElement + splicedElement);
          });
        return capitalizedSentence.join(" ");
      }
- .map(), toLowerCase(), split(), slice(), toUpperCase(), push() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        sentence
          .toLowerCase()
          .split(" ")
          .map(word => {
            capitalizedSentence.push(word[0].toUpperCase() + word.slice(1));
          });
        return capitalizedSentence.join(" ");
      }
- .map(), toLowerCase(), split(), toUpperCase(), push(), replace() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        sentence
          .toLowerCase()
          .split(" ")
          .map(word => {
            let newWord = word.replace(word[0], word[0].toUpperCase());
            capitalizedSentence.push(newWord);
          });
        return capitalizedSentence.join(" ");
      }
- .map(), toLowerCase(), split(), toUpperCase(), replace() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        capitalizedSentence = sentence
          .toLowerCase()
          .split(" ")
          .map(word => {
            return word.replace(word[0], word[0].toUpperCase());
          });
        return capitalizedSentence.join(" ");
      }
- .map(), toLowerCase(), split(), toUpperCase(), slice() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        capitalizedSentence = sentence
          .toLowerCase()
          .split(" ")
          .map(word => {
            return word[0].toUpperCase() + word.slice(1);
          });
        return capitalizedSentence.join(" ");
      }
- for...of, toLowerCase(), split(), toUpperCase(), splice(), push(), spread operator, and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        let newSentence = sentence.toLowerCase().split(" ");
        for (word of newSentence) {
          let newWord = word[0].toUpperCase() + [...word].splice(1).join("");
          capitalizedSentence.push(newWord);
        }
        return capitalizedSentence.join(" ");
      }
- for...in, toLowerCase(), split(), toUpperCase(), splice(), push(), spread operator and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        let newSentence = sentence.toLowerCase().split(" ");
        for (word in newSentence) {
          let newWord =
            newSentence[word][0].toUpperCase() +
            [...newSentence[word]].splice(1).join("");
          capitalizedSentence.push(newWord);
        }
        return capitalizedSentence.join(" ");
      }
- forEach...loop, toLowerCase(), split(), toUpperCase(), replace(), push() and join()
 
      function sentenceCapitalization(sentence) {
        let capitalizedSentence = [];
        let newSentence = sentence.toLowerCase().split(" ");
        newSentence.forEach(element => {
          let newWord = element.replace(element[0], element[0].toUpperCase());
          capitalizedSentence.push(newWord);
        });
        return capitalizedSentence.join(" ");
      }
Conclusion
There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.
If you have questions, comments or suggestions, please drop them in the comment section.
Up Next: Algorithm 101: 6 Ways to Check if a Word is a Palindrome
You can also follow and message me on social media platforms.
Thank You For Your Time.
    
Top comments (0)