I need help with a coding challenge involving sentences and arrays.
The task is to replace specific words or phrases in a sentence with HTML tags for user interaction. Here's an example using JavaScript:
var sentence = "Yes. I know him from my neighborhood. Come in!",
phrases = ["YES", "I", "HIM", "NEIGHBORHOOD", "COME IN"],
result = "";
The goal is to wrap each matching word or phrase in the sentence with span tags, allowing users to click on them for more information. The challenge is to do this efficiently within a single loop without relying on regular expressions or additional loops due to potential conflicts with smaller words like "I" or articles like "A" and "AN".
// expected output
result = "<span>Yes</span>. <span>I</span> know <span>him<span> from my <span>neighborhood</span>. <span>Come in</span>!";