javascript - 我需要关于多次重复使用 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()
方法上,如果玩家输了,这个方法会显示红屏,如果玩家赢了,会显示绿屏,我可以选择一个h1
ID#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
解决方案
我猜你的else if
情况从来不匹配
代替
this.checkForWin === true
(这总是错误的,因为它是一个函数)
你想检查实际的返回值,所以你必须执行它
this.checkForWin() === true
您可能已经发现自己没有通过使用例如console.log("xyz")
调试来执行
推荐阅读
- boto3 - 使用 boto3 创建与 AWS Glue 的连接时,可用区出现 InvalidInputException 错误
- python - 追加到数组pymongo
- gatsby - 仅使用 Gatsby Link 更改内容
- python - spyder中从excel手动复制粘贴到python代码的想法
- performance - XMITQ 上的 MQ 慢出队速率
- html - CSS - 在 DIV 中对齐文本、输入和图像
- sql - 以下预期输出的 SQL 查询
- facebook-graph-api - Facebook messenger webhook 没有收到任何消息
- python - 当我只尝试更改一个时,为什么我的代码会更改多个列表元素?
- c# - devexpress 魔术。添加新列会导致从未添加过的不同字段名