1

var sentence = 'The quick brown fox jumped over the lazy dog.';

var word = ['fox'];

console.log('The word "' + word + (sentence.includes(word)? '" is' : '" is not') + ' in the sentence');
// expected output: "The word "fox" is in the sentence"

This is the String.includes() in the Javascript docs but what I want to do is something like this:

var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ['fox', 'dog'];

console.log('The word "' + word + (sentence.includes(word)? '" is' : '" is not') + ' in the sentence');

I want to test an array of strings to the sentence and return true if one of the strings in the array are in the sentence. But as you can see in the snippet, it does not work.

2
  • 2
    You need to loop. The first version works because ["fox"].toString() is "fox" - the second not because ['fox', 'dog'].toString() is "fox,dog" Commented Oct 17, 2018 at 7:57
  • well, you are checking if an array is inside a string, you could use some on word instead Commented Oct 17, 2018 at 7:57

4 Answers 4

2

I assume it prints for each item in variable word So like

 "The word "fox" is in the sentence"
 "The word "dog" is not in the sentence"

You can do

var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ["fox", "dog"]

function test(word) {
  console.log('The word "' + word + (sentence.includes(word) ? '" is' : '" is not') + ' in the sentence');
}
word.map((item) => {
  return test(item)
})

Sign up to request clarification or add additional context in comments.

Comments

1

try this one instead, if one of the var word does not match at the sentence, it'll return false else if everything is included then it will return true.

var sentence = 'The quick brown fox jumped over the lazy dog.'
var word = ["fox", "the", "lol"]

var initialReturn = true

check = (word) => {
  !sentence.includes(word) ? initialReturn = false : null
}

word.map(item => {
  return check(item)
})


console.log("initialReturn", initialReturn)

Comments

0

var sentence = 'The quick brown fox jumped over the lazy dog.';
var word = ['fox', 'dog'];

console.log('The word "' + word + (sentence.split(' ').some(d => word.includes(d)) ? '" is' : '" is not') + ' in the sentence');

4 Comments

Your array shows that the code does not work as requested. Please test in the snippet I made for you. OP asked return true if one of the strings in the array are in the sentence
It works. Pls check it again. Do not miss the word array, I changed that
It fails with var word = ['foxs', 'dog'];
It only returns true when both of the items in the array are present but returns false when one of them is not in the sentence
0

Check this piece of code. I learned one important thing working with strings. Regular expressions can help you (but it's hell) and use extensions (functions, helpers) in your framework is you're using it. But if you use native js indexOf will help you. I updated my answer a bit.

var sentence = 'The quick brown fox jumped over the lazy dog.';
var words = ['fox', 'dog'];

console.log('The words: "' + words + (sentence.split(/[ ,.]/).some(function(e) { return words.indexOf(e) >= 0 })? '" is' : '" is not') + ' in the sentence');

2 Comments

It's quite simple to be explained. It doesn't fail
I see - it works, but still give a short explanation. SO works better when there is an explanation - for example your code DOES return true if one is present and one is not, but that was not obvious

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.