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.
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)) {
...
}