我试图制作一个文字克隆,但可以使其正常运行。我可以做些什么以使其起作用?

发布于 2025-01-24 22:10:44 字数 1462 浏览 3 评论 0原文

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"
      }

    
    
  }
}
  
})

每当我在空间中输入一些字母并按下Enter按钮时,其他行中正方形的颜色会发生变化,并且似乎是以对角线方式的。我尝试处理IF语句,并将其更改为其他IF语句,我觉得这与为什么Wordle Clone不起作用有关,但我仍然不确定。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殊姿 2025-01-31 22:10:45

我认为问题在这里:

 word.indexOf(current) == false

索引返回索引或-1。

-1 == false // false

I think the problem is here:

 word.indexOf(current) == false

indexOf returns the index or -1.

-1 == false // false
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文