0

I want to find out if a certain word is in a given sentence. If yes, then I want to return true, if not, then return false.

For example:

word = "big"

sentence 1: "Tom is my big brother."

This should return true.

sentence 2: "Sandra is very old."

This should return false.

sentence 3: "Ralph is the biggest."

This should return true.

How can I find out if a word is in a given sentence?

Marcel Kämper
  • 304
  • 5
  • 16
Bryan
  • 187
  • 5

1 Answers1

2

You can use built in string method to find out a substring (word) in the given string (sentence).

The includes() method can be used to determine if a string is present inside other string, returning true or false.

Usage:

If you are using ES6 you can use the includes method:

'a nice string'.includes('nice') //true
 const isPresent = sentence.includes(word)  //returns Boolean values

If you are not using ES6, you can use the indexOf method:

'a nice string'.indexOf('nice') !== -1 //true
Ryker
  • 792
  • 4
  • 11
cefeboru
  • 332
  • 3
  • 4
  • 12