small medium large xlarge

01 Mar 2018, 19:58
guy dude guy (6 posts)

I’m working on a tic-tac-toe project, and I need a function to compare boards that have been rotated or reflected. So far, this is what I’ve got: Click to See

I put console.log()’s everywhere to see stuff, but I don’t get why, even after a match, the yes variable is still undefined instead of "finished".

If you could help, or have a different way of finding these matches, please notify me. Help is greatly appreciated!

02 Mar 2018, 01:08
Chris Strom (277 posts)

The problem is that forEach() always returns undefined – no matter you have inside the function.

The easiest way to get this working is to declare yes before the forEach() and then assign it directly inside the forEach():


var yes;
board1.forEach(function (b) {
  if (b !== board2[v[n]]) same++;
  console.log(b + ", " + board2[v[n]] + ", " + v[n] + ", " + variationNum + ", " + same);
  if (n === 8 && same === 0) {
    yes == "finished";



Hope that helps!


You must be logged in to comment