-6

I have a variable thread.particpants[0].name which is a string. It can contain strings of characters like this: "Jean Jean" or chains like that "Jean".

I would like to know when this chain is composed of 2 words, and when it is composed of 1 word

Floriane
  • 315
  • 6
  • 20
  • Hint: Try checking if the string contains a space. – Jordi Nebot Nov 30 '17 at 10:55
  • Do the words have to be the same to count? Is it only a space that will ever be a word boundary? What if there are more than 2 words? And most importantly... what have you tried so far? – musefan Nov 30 '17 at 10:58

2 Answers2

1

Trim the string and then check for space

var hasMultipleWords = input.trim().indexOf( " " ) != -1; //returns true if there is a space
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

Check if contains a space

thread.particpants[0].name.indexOf(" ")

Or, alternatively, split on space and check the array length

thread.particpants[0].name.split(" ").length
Varkal
  • 1,336
  • 2
  • 16
  • 31