Created
November 16, 2020 03:22
-
-
Save nfamula/51dede6fea50050c843c3a9991b49140 to your computer and use it in GitHub Desktop.
is the string in the dictionary?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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