Incorrect syntax for declaring css styles fixed. Attributes should be able to contain the entire css style string. You can use the template syntax to insert the word variable into your desired HTML string.
Use the index (idx) to get the position in your apiSuggestion array that you are comparing to.
// grab text content and split into array with ' ' as delimiter
let initial = document.getElementById('initial').textContent.split(' ');
// your output suggests that "bonker" was a typo for "bonkers"?
let apiSuggestion = ["something", "else", "went", "bonkers"];
let matchMaker = initial.map(function(word, idx){
if( word !== apiSuggestion[idx] ){
return `
<span style="background-color:black; color:red">
${word}
</span>
`
} else {
return word;
}
});
// escaped space ( ) because spaces will get collapsed between HTML elements
document.getElementById("suggestion").innerHTML = matchMaker.join(' ');
<div> Initial:<p id="initial">somthng else wnet bonkers</p></div>
<div> Fix this typo: <p id="suggestion">somthng wnet</p></div>
style=“ background-color:black; color:red;>
Unclosed quote, and you don’t actually need this style anymore since you are highlighting words individually
style=”background-color:black”; color:”red”>
This is interpreted as attributes:
style
=”background-color:black”;
color:"red"
Hopefully the mapping code is self explanatory. You weren’t returning the value that each element should be “mapped” (or in other words transformed) to.