为什么我的 wordle 克隆不能在 code.org 中将正确答案设置为绿色?

发布于 2025-01-16 00:01:24 字数 824 浏览 1 评论 0原文

因此,我使用 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 技术交流群。

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

发布评论

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

评论(1

月亮是我掰弯的 2025-01-23 00:01:24

我希望 getColumn 将具有多个结果的数组分配给 answers

您的 wordbutton 事件正在将该数组与 guess(一个字符串)进行比较。因此,if (guess ==answers) { 行可能是一个错误。也许您的意思是,if (guess ==answers[index]) {

此外,您还根据答案将整个单词着色为红色或绿色。如果您的目的是一次检查一个字母,您的代码可能看起来更像:

let answer=answers[index];
if (guess.charAt(i) == answer.charAt(i)) {
    ...
}

I would expect getColumn to assign an array with multiple results to answers.

Your wordbutton event is comparing that array to guess, which is a string. So, the line if (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:

let answer=answers[index];
if (guess.charAt(i) == answer.charAt(i)) {
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文