codesONLY / JavaScriptONLY

Knowledge Resource of core fundamentals of JavaScript explained in simple way!
378 stars 182 forks source link

✨ Add Algorithm for Capitalizing First Character of Every Word #109

Closed VinayakVispute closed 7 months ago

VinayakVispute commented 8 months ago

PR Description

I want to add a JavaScript algorithm that capitalizes the first character of every word in a text string.

Algorithm

  1. Split the input text into an array of words based on spaces.
  2. Create an empty array to store the capitalized words.
  3. For each word in the array of words, do the following: a. If the word has a length greater than 0:
    • Get the first character of the word and capitalize it (convert to uppercase).
    • Get the rest of the word and convert it to lowercase.
    • Concatenate the capitalized first character and the lowercase rest of the word.
    • Add the resulting word to the array of capitalized words. b. If the word is empty, add it as is to the array of capitalized words.
  4. Join the capitalized words into a single string using spaces as separators.
  5. Return the resulting string with the first character of every word capitalized.
VinayakVispute commented 8 months ago

@sohamsshah Please Review my PR 😊