I am working on a word frequency app, where the user can find the most used word in the text and alter that word and display an altered version of the whole text in HTML.
I created a function where you create a span tag around the altered word, so I can give a class to add some CSS to highlight it.
const updatedTextArr = text.map(word => {
const isSameWord = word === mostUsedWord
if (isSameWord) {
const spanNode = document.createElement("span")
const alteredWord = `foo${mostUsedWord}bar`
spanNode.append(alteredWord)
word = spanNode
return word
} else {
return word
}
const createString = updatedTextArr.join(" ").toString()
setTextData(createString)
})
But in this way, when I console.log it, the returned altered word in a text array shows as "HTML element"
and when I display the "createString", it shows altered words as same as "HTML elements" in the view.
I wonder what is the better way to add a span tag to the found most used word?