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;
}

Эта проблема очень популярна на собеседованиях по кодированию.

Продолжайте учиться, продолжайте расти!

Не забудьте подписаться на меня, чтобы получать больше таких статей, и подписывайтесь на нашу рассылку.

Подключаемся по LinkedIn!. Прочтите дополнительную информацию по вопросу о структуре данных javascript.