为什么我的 wordle 克隆不能在 code.org 中将正确答案设置为绿色?
因此,我使用 code.org wordle 数据集重新创建 wordle 克隆。问题是,所有代码都工作正常,但当您输入正确答案时,它会显示为红色(应该是绿色)。如果有人可以看一下我的代码并看看有什么问题,我将不胜感激。
//Getting Wordle Answer
var answers = getColumn("Wordle", "validWordleAnswer");
var index = (randomNumber(0, answers.length));
console.log(answers[index]);
var letters = ["letter1", "letter2", "letter3", "letter4", "letter5"];
//Checking Words
onEvent("wordbutton", "click", function( ) {
var guess = getProperty("wordInput", "text");
for (var i = 0; i < 5; i++) {
if (guess == answers) {
setProperty(letters[i], "background-color", "green");
} else {
setProperty(letters[i], "background-color", "red");
}
setProperty(letters[i], "text", guess[i]);
setProperty("wordsUsedOuput", "text", guess[i]);
}
});
特别是第 5 行和第 6 行,不知道问题是什么。
So I am using the code.org wordle dataset to recreate a wordle clone. The problem is, all the code works fine yet when you input the right answer, it comes out as red (supposed to be green). If someone could take a look at my code and see what is wrong that would be greatly appreciated.
//Getting Wordle Answer
var answers = getColumn("Wordle", "validWordleAnswer");
var index = (randomNumber(0, answers.length));
console.log(answers[index]);
var letters = ["letter1", "letter2", "letter3", "letter4", "letter5"];
//Checking Words
onEvent("wordbutton", "click", function( ) {
var guess = getProperty("wordInput", "text");
for (var i = 0; i < 5; i++) {
if (guess == answers) {
setProperty(letters[i], "background-color", "green");
} else {
setProperty(letters[i], "background-color", "red");
}
setProperty(letters[i], "text", guess[i]);
setProperty("wordsUsedOuput", "text", guess[i]);
}
});
Specifically line 5 and 6, confused as to what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我希望
getColumn
将具有多个结果的数组分配给answers
。您的
wordbutton
事件正在将该数组与guess
(一个字符串)进行比较。因此,if (guess ==answers) {
行可能是一个错误。也许您的意思是,if (guess ==answers[index]) {
?此外,您还根据答案将整个单词着色为红色或绿色。如果您的目的是一次检查一个字母,您的代码可能看起来更像:
I would expect
getColumn
to assign an array with multiple results toanswers
.Your
wordbutton
event is comparing that array toguess
, which is a string. So, the lineif (guess == answers) {
is likely a bug. Perhaps you meant,if (guess == answers[index]) {
?Also, you're coloring the whole word red or green based on the answer. If your intent is to check a letter at a time, your code might look a little more like: