IF statement not executing

The below code is from my Wordle Clone project. The code contains the function which evaluates whether the user has won or lost the game. There are two outcomes for that :
(1) The guess entered by the user matches with the word for the day (aka targetWord).
(2) The user has lost all the six chances given.

The first IF statement, checking if the guess and the targetWord are matching, isn’t getting executed even when the condition is true. Any idea what the bug might be???

The guess and targetWord are all getting passed properly into the function, the type is also the same and I even tried doing:

if (guess.toLowerCase() === targetWord.toLowerCase() ) to make sure the cases of both the words are the same.

There really is no way we can give you good help unless we can see the actual entire code. And not a picture please, a link to the actual code. For example, the first if statement:

if (guess == targetWord) {

I know where guess is coming from since it is passed into the function as a parameter, but I have no idea where you are getting targetWord, so there is really no way I could tell from this code why this if statement isn’t triggering other than to tell you what you should already know, that guess never equals targetWord.

This statement doesn’t make sense. If it is true then the if statement is executing. Again, without seeing your entire code it is almost impossible to tell why this statement is never true. You might do a console.log on both of those variables before the if statement to verify that they are holding the values you think they should be holding.

If they really are the same value then my guess would be that one of the functions in the if block has an error and is causing your code to stop executing.

Link of Project

I have demarcated the possible bug portions by a series of forward slashes like this ( ////…).

The first demarcated part deals with the submission of the guess by the user. The bug there is that the words present in the wordlist aren’t getting accepted by the code. In order to see what happens with the code when the word gets accepted, I had to remove the NOT operator from the condition placed on line 5881 in the given link. Mentioning all this as the function checkWinLose (the function that I needed help in debugging) gets called in this part of the code.

The second demarcated part (Mentioned as Part to be debugged) is where the above code snippet is from.

Link for the project

I just realized that the function used for the grid animation when the user wins the game is missing from the code. Will re-write that function and will get back to you. Thank you for the help!

Hello! I have updated the code with the concerned animation function. The IF statement is still not getting executed.

Link of Wordle Clone project

Hi,
The problem is in your guess word. The guess word comes out to be " happy" which is not equal to target word which is “happy”, because in guess word there is whitespace in the starting. You need to trim your whitespace by using guess.trim(). I used trim as shown below in your code and it works perfectly.

if (WORDS.indexOf(guess.trim())==-1) {
// if word isn’t present in list
showAlert(“Not in word list!”); //giving alert
shakeTiles(activeTiles); //indicates the error
return;
}
I guess the space comes out because of your reduce function, you should check that too.

1 Like

Thank you so much !!! It worked. :grinning:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.