我有两个数组,每个数组都被映射过来,如果数组与“apiSuggestion”不匹配,则元素会得到黑色的背景色和红色文本。这适用于:
let sentenceOne = ["somthing", "life", "nothing"];
let sentenceTwo = ["nummber", "seven", everythng"];
let apiSuggestion = ["something", "life", "nothing", "number", "seven", "everything"];
let matchMakerOne = sentenceOne.map(function(word,index){
if( word !== apiSuggestion[index]){
return `
<span style="background-color:black; color:red">
${word}
</span>
} else {
return word
}
})
document.getElementById("sentenceOne").innerHTML = machMakerOne.join(' ')
//in the HTML i see "somthing" highlighted in black background-color with red text, as it doesnt match to api suggesstion
即使逻辑相同,这也不起作用,我在哪里犯了错误?
let matchMakerTwo = sentenceTwo.map(function(word,index){
if( word !== apiSuggestion[index]){
return `
<span style="background-color:black; color:red">
${word}
</span>
} else {
return word
}
})
document.getElementById("sentenceTwo").innerHTML = machMakerTwo.join(' ')
// "nummber", "seven", everythng" all get black background and red text
// nummber and everythng should get black background and red text
我试图“连接”句子一个和句子两个,它只捕获第一个数组的拼写错误,而不是第二个数组。如何突出显示这两个数组中的拼写错误并将其呈现在 HTML 中?任何帮助是值得赞赏的。提前感谢您。我创建了JS小提琴
ITMISS
素胚勾勒不出你
相关分类