December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 2 | Checking a String for Specific Words with includes() |
Suppose you’re building an online bakeshop and want to filter messages so they can be routed to the correct baker. You need to check the strings in incoming emails to account for different spellings of words like “doughnut” versus “donut.” You can’t use the includes() method alone because it allows you to look for only a single word.
Put the words you want to search for in an array. Then create a function that accepts two arguments: a string to search and an array of words. Inside the function, search for each word in the string and return true if at least one search is successful:
| | const msg = "I'd like to order two donuts" ... |
Read now
Unlock full access