JavaScript | Print first letter of every word in the string

Input: S = "bad is good"
Output: big


1. Traverse string str. And initialize a variable v as true.
2. If str[i] == ' '. Set v as true.
3. If str[i] != ' '. Check if v is true or not.
a) If true, copy str[i] to output string and set v as false.
b) If false, do nothing


function firstLetterWord(str) {   let result = "";   // Traverse the string.   let v = true;   for (let i = 0; i < str.length; i++) {       // If it is space, set v as true.      if (str[i] == ' '){        v = true;      }      // Else check if v is true or not. If true, copy character in output string and set v as false.     else if (str[i] != ' ' && v == true) {         result += (str[i]);         v = false;     }  }  return result;}

This problem is very popular in the coding interviews.

Keep learning, keep growing!

Don’t forget to follow me for more such articles, and subscribe to our newsletter.

Let’s connect on LinkedIn!. Please read more for more data structure javascript question.