Home > Blockchain >  JavaScript if statement able to compare two variables with identical values
JavaScript if statement able to compare two variables with identical values

Time:12-09

I am creating a recreation of Wordle (6x6 grid however) where the the row is sent as an array through a socket. I am able to check if the letter is correct, but the issue arises when I'm checking for duplicates before assigning something to yellow (the letter exists but it is currently in the wrong place). I am iterating through the places other than the current index and checking to see if that letter has been deemed as valid yet. For this wordle I only have one word, which is "cancer". I have included the code below. My problem is it can't varify. Any help would be appreciated.

dupCheck == wordPoint
socket.on('check wordle', (check) => {
        var word = "cancer";
        var feedback = [];

        console.log(check)

        for(var i = 0; i < check.length; i  )
        {
            var letter = check[i];
            var wordPoint = word.substring(i, i 1);
            
            if(word.includes(letter))
            {
                if(letter == wordPoint)
                {
                    feedback.push("green");
                }
                else
                {
                    var duplicate = false;
                    
                    for(var j = i; j < check.length; j  )
                    {
                        var dupCheck = check[j];
                        console.log(dupCheck   " j "   wordPoint)
                        if(dupCheck == wordPoint)
                        {
                            duplicate = true;
                            console.log(j)
                            console.log("j")
                            break
                        }
                        else
                        {
                            console.log("It reads this")
                        }
                        console.log(duplicate)
                    }

                    for(var k = 0; k < i; k  )
                    {
                        var dupCheck = check[k];
                        console.log(dupCheck   " k "   wordPoint)
                        if(dupCheck == wordPoint)
                        {
                            duplicate = true;
                            console.log(k)
                            console.log("k")
                            break
                        }
                    }
                    
                    if(duplicate)
                    {
                        feedback.push("grey");
                    }
                    else
                    {
                        feedback.push("yellow");
                    }
                }
            }
            else
            {
                feedback.push("grey");
            }

        }

        console.log(feedback)
        
        io.emit('wordle feedback', feedback);
    })

Whenever I test in the console output, I can see that the two values are identical.

CodePudding user response:

I don't know how a socket is set up or how it is invoked, but I can see that there is an unnamed function in the center of your snippet. I created a similar function here that will do the necessary comparisons and return "grey", "yellow" or "green" for each character of a guessed word:

const word ="cancer";

function wordle(test){
 const w=word.split("");
 return test.split("").map((c,i)=>{ let j;
  if(c==w[i]) {
   w[i]="*";
   return "green";
  } else if ((j=w.indexOf(c))>-1) {
   w[j]="*";
   return "yellow";
  } else return "grey";
 })
}
["jackal","jacket","saucer"].forEach(test=>
console.log(test,wordle(test)))

  • Related