Need help with this one

Hi All,

I can’t figure this out.

On line 30 the console.log should only trigger when the two pairs are !=

But it immediately triggers on the first cell click. I’m sure there must be a super simple explanation. What’s going on? How do i fix this?

May I ask what that second for loop in your first for loop is doing?

Could it be that the logical question the if statement is posing: “Is the first item in pairs_array not the same as the second item?” Well on the first pass you only have 1 item in the array so far. So it’s asking: " Is item 1 (cell.innerHTML) not the same as item 2 (undefined because it doesn’t exist)?" Which would evaluate to true.

Thanks kylec, this help a lot. I have moved the second for loop. I only can’t figure out how to get the logical to work. This is what I tried:

cell.innerHTML = cellClicked[i];
let pairOne = pairs_array[0];
let pairTwo = pairs_array[1];

if(pairOne == pairTwo) {
else if(pairTwo != pairOne) {
   console.log("Try again!")

This now checks for item1 (pairTwo) which does exist to check if != (pairOne), so would not evaluate to true? I’m confused about this one.

Ok, so i’ve found a way around this. It works but i feel that it’s a bit buggy. How can this be improved?

else if(pairTwo) {
   console.log("Try again!")
   pairs_array = [];

You might be able to try wrapping the statements in a check to ensure the second item exists first:

if (pairs_array[1]) {
    if (pairs_array[0] == pairs_array[1]) console.log("Match")
    else console.log("Try Again!")
1 Like