67.searchMessages

Write a function named searchMessages that receives 2 parameters:

  • an Array of messages, each with 2 properties:
    • body - the text inside the message
    • sentAt - a Date representing when the message was sent
  • a string representing a searchTerm

The function should find all messages that match the searchTerm. A message matches it if:

  1. the search term is a single word and that word is included in the body
  2. the search term contains multiple words separated by spaces and at least one of them is included in the body

Then return all those messages in descending order based on:

  1. how many letters of the searchTerm they match
  2. the sentAt property (newest first)

NOTE: the search is not case sensitive

Example 1

Input

messages

=

searchTerm

=

Output