2

In ReactJS tutorial has a tutorial on building a tic tac toe game with React. There's this function that checks to see if a winning move is made.

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

it works but the for loop could be improved to use for x in lines like so

function calculateWinner(squares) {
  const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let line in lines) {
    const [a, b, c] = line;
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
      return squares[a];
    }
  }
  return null;
}

but when I tried that it didn't find a winning so I resorted to the older code.

I'm not sure why though the second edit failed.

2 Answers 2

2
function calculateWinner(squares) {
    const lines = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
    for (let line in lines) {
        console.log(line);
        const [a, b, c] = line;
        if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
            return squares[a];
        }
    }
    return null;
}

Run this code. Your line variable is index of array, not inner array (e.g. [0, 1, 2]).

EDIT:

As kristaps mentioned in comment proper solution is let line of lines (of instead of in).

Sign up to request clarification or add additional context in comments.

2 Comments

Your code still says let line in lines, did you mean let line of lines?
I just pointed out why the code did not work properly. You are right.
0

here in your first code you are using for loop which can excess 2d array in proper way example below program will produce out put like

    [
      0,
      1,
      2
    ] 

const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let i = 0; i < lines.length; i++) {
    const [a, b, c] = lines[i];
    console.log(lines[i]);
    }

and also for each loop is use for excess each element at a time for example below program will produce output like

    1
    2
    3

as it

const lines = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];
  for (let line in lines) {
    const [a, b, c] = line;
    console.log(line)
    }

i think you can understand what happened here

2 Comments

yes and to return the content not the index it should be for (let line of lines)
yah that a point for each loop is use for each element at a time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.