我试着做一个世界克隆,但不能让它正常工作.有什么我能做的吗?


let body = document.querySelector("body")
let container = document.querySelector("#container")
let enter = document.querySelector("#enter")

let inputArr = []
for (let rowCount = 0; rowCount < 6; rowCount++) {
let row = []
inputArr.push(row)
for (let i = 0; i < 5; i++) {
let space = document.createElement("input");
container.appendChild(space)
row.push(space); 
space.classList.add("square")
}
}

let wordSet = ["panic", "knobs", "swain", "dupes", "venom", "great", "carom", "soare"]
let num = Math.floor(Math.random()*8)
let word = wordSet[num]
console.log(word)
enter.addEventListener("click", () => {
for (let rowVal = 0; rowVal < 6; rowVal++) {
for (let col = 0; col < 5; col++) {
let current = inputArr[rowVal][col].value

if (current === "" || inputArr[rowVal][col].style.backgroundColor !== "darkgray") {
rowVal++
}

if (current == word[col]) {
inputArr[rowVal][col].style.backgroundColor = "green" 
}
if (current !== word[col] && word.indexOf(current)) {
inputArr[rowVal][col].style.backgroundColor = "yellow"
}
if (current !== word[col] && word.indexOf(current) == false) {
inputArr[rowVal][col].style.backgroundColor = "white"
}


}
}

})

每当我在空格中输入一些字母并按下回车键时,其他行中正方形的颜色就会改变,并且看起来是对角线样式。我试着乱搞if语句,并将它们更改为else if语句,我觉得这可能与为什么世界克隆不工作有关,但我仍然不完全确定。

我认为问题在这里:

word.indexOf(current) == false

indexOf返回索引或-1。

-1 == false // false

相关内容

  • 没有找到相关文章

最新更新