Reputation: 11
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"
}
}
}
})
Whenever I type in some letters into the spaces and press the enter button, the colors of squares in other rows change, and it seems to be in a diagonal fashion. I tried messing around with the if statements, and changing them to else if statements, and I feel like that could have to do with why the wordle clone isn't working, but I'm still not entirely sure.
Upvotes: 1
Views: 39
Reputation: 97
I think the problem is here:
word.indexOf(current) == false
indexOf returns the index or -1.
-1 == false // false
Upvotes: 0