Skip to content

Instantly share code, notes, and snippets.

@nfamula
Created November 16, 2020 03:22
Show Gist options
  • Save nfamula/51dede6fea50050c843c3a9991b49140 to your computer and use it in GitHub Desktop.
Save nfamula/51dede6fea50050c843c3a9991b49140 to your computer and use it in GitHub Desktop.
is the string in the dictionary?
const dictionary = ["hello", "world", "grey", "rainy"];
const phrase = "hello world, it’s a grey and rainy day today!";
//remove punctation by using a regex
const newPhrase = phrase.match(/[^_\W]+/g).join(' ');
//console.log(newPhrase);
//split the phrase on the spaces
const words = newPhrase.split(" ");
//console.log(words);
//filter out any words that don’t match
const matchingWords = dictionary.filter(word => words.includes(word));
//console.log(matchingWords);
const NumOfMatchingWords = matchingWords.length;
console.log(`${NumOfMatchingWords} words match!`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment