Javascript String Find bad word

Description

Javascript String Find bad word


/*/*from   www.j a v  a2 s . co m*/
   Implement the `hasBadwords` function in the code below to return a boolean if the
   message contains a badword in it or not.  A badword is contained in the message
   if the word appears in the sentence, ignoring adjacent punctuation and case.
*/
 
var sentences = [
   "this is a test. window this is a test.",
   "chair this is a test,",
   "test test.",
   ".test",
   ".test",
   "!"
];
 
var badwords = ['window', 'chair', 'test'];
 
// Fill in function body here
var hasBadwords = function (message, index) {
  var splitMessage = message.split(/[\s+!?,.\;]/)
  for (var i = 0; i < splitMessage.length; i++) {
    if(badwords.indexOf(splitMessage[i].toLowerCase()) !== -1) {
      return true
    }
  }

  return false
}

// Tell us what the output is from running this code:
console.log(sentences.map(function (sentence, index) {
   return hasBadwords(sentence) ? index : '';
}).join(''));



PreviousNext

Related