Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How to compare array with words and string, but string is sentence

help me please How to compare string and array if there string in array return true, else false string it in not one word, it is sentence

Here is my code

const keyWords = [
  "Hello",
  "hello",
  "help please",
  "Help please",
  "John",
  "john",
  "need",
  "Need"
]

var messageUser = "My name is Peter and I need help";

function check(str) {
  let newMessageUser = str.split(" ")

  for (let i = 0; i <= keyWords.length; i++) {
    for (let j = 0; j <= newMessageUser.length; j++) {
      let answer = (keyWords[i] === newMessageUser[j]) ? true : false;

      return answer
    }
  }
}

console.log(check(messageUser))
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

you can use

  • array.some to check if one entry match a condition

  • string.includes to check if word is include in string

const keyWords = ["Hello", "hello", "help please", "Help please", "John", "john", "need", "Need"]

var messageUser = "My name is Peter and I need help";

function check(str) {
  return keyWords.some(word => messageUser.includes(word));
}

console.log(check(messageUser))

about 4 years ago · Juan Pablo Isaza Report

0

const keyWords = [
    "Hello",
    "hello",
    "help please",
    "Help please",
    "John",
    "john",
    "need",
    "Need"
]

const messageUser = "My name is Peter and I need help";

const results = keyWords.map((item) => {
    return messageUser.toLowerCase().includes(item.toLowerCase());
});

console.log(results);

about 4 years ago · Juan Pablo Isaza Report

0

You can achieve that via single line of code.

const keyWords = [
  "Hello",
  "hello",
  "help please",
  "Help please",
  "John",
  "john",
  "need",
  "Need"
]

var messageUser = "My name is Peter and I need help";

console.log(keyWords.map(item => messageUser.includes(item)))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!