首页 > 解决方案 > 我需要关于多次重复使用 ID 的帮助

问题描述

所以我正在做一个猜词游戏,

在此处输入图像描述

在我的 Game.js 文件中,我有一个名为 Game 的类,它有一个名为checkForwin()检查玩家是否选择了游戏中的所有字母的方法,它根据console.log我看到的消息工作

checkForWin() {
    //phraseMatch is the length of the empty boxes
const phraseMatch  = $('.letter').length;
//match is the length of the displayed boxes with the class of show
const match  = $('.letter.show').length;
//if the length of the empty boxes is equal to the length of the displayed boxes then return true
  if (phraseMatch === match) {
      console.log(phraseMatch + match + ' (the player has chosen all letters in the phrase')
    return true
  }

}

我坚持的地方虽然是在一个名为 的gameOver()方法上,如果玩家输了,这个方法会显示红屏,如果玩家赢了,会显示绿屏,我可以选择一个h1ID#game-over-message来更改消息和当玩家失败时,通过添加类('lose a')来显示屏幕颜色,但是当我尝试通过说第二次使用相同的 id 时

else if (this.checkForWin === true || this.missed < 5)  {
        $('#game-over-message').text('You won!');
        $('#overlay').show().addClass('win a');
        $('#btn__reset').text('Try Again!');



    }

我似乎无法通过添加我的 css 文件中的类('win a')来访问 id 以显示获胜颜色

gameOver() {
    //if the player misses 5 times, display the game over message from index.html
    if (this.missed === 5) {
        $('#game-over-message').text('Game Over You Lost'); 
        $('#overlay').show().addClass('lose a');
        $('#btn__reset').text('Try Again!!');






    } else if (this.checkForWin === true || this.missed < 5)  {
        $('#game-over-message').text('You won!');
        $('#overlay').show().addClass('win a');
        $('#btn__reset').text('Try Again!');



    }

}

有人可以帮忙吗?这是我的参考仓库 https://github.com/SpaceXar20/FSJS-techdegree-project-4-b

标签: javascript

解决方案


我猜你的else if情况从来不匹配

代替

this.checkForWin === true(这总是错误的,因为它是一个函数)

你想检查实际的返回值,所以你必须执行它

this.checkForWin() === true

您可能已经发现自己没有通过使用例如console.log("xyz")调试来执行


推荐阅读