# Need help understanding React Tic Tac Toe "Winner" function

**URL:** <https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 23, 2017, 6:29pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840 "2017-07-23T18:29:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriswu](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@chriswu](https://forum.freecodecamp.org/u/chriswu)\
**Post date:** [July 23, 2017, 6:29pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/1 "2017-07-23T18:29:38Z")

</div>

Can someone please help me understand the “Winner” helper function from the React Tic Tac Toe tutorial?

This is taken from the official React tutorial. The problem I have is understand the for loop. Thanks.

[https://facebook.github.io/react/tutorial/tutorial.html](https://facebook.github.io/react/tutorial/tutorial.html)

```
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;
}

```

---

<div class="post-metadata">

**Author:** ![angelhdz12](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@angelhdz12](https://forum.freecodecamp.org/u/angelhdz12)\
**Post date:** [July 23, 2017, 11:10pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/2 "2017-07-23T23:10:17Z")

</div>

Hello.

First, calculateWinner() function, declares a multidimensional array ‘lines’, that holds all the winning combinations.

Second, a ‘for’ loop iterate through all the combinations, and adds the board’s squares indexes in constants ‘a’, ‘b’ and ‘c’.

Finally, and ‘if’ conditional statement will compare the current combination of the iteration with the board’s clicked squares combinations, and if there is a match, it returns the current combination, otherwise it returns null.

This is my first time on this forum. Hope I could help you.

---

<div class="post-metadata">

**Author:** ![chriswu](https://avatars.discourse-cdn.com/v4/letter/c/47e85d/32.png) [@chriswu](https://forum.freecodecamp.org/u/chriswu)\
**Post date:** [July 24, 2017, 2:28am UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/3 "2017-07-24T02:28:23Z")

</div>

It didn’t help ☹

But I did managed to figure out while I was showering. The test is on the value or the props. If the value of a, b, c are the same, return the value of a (either X or O).

Thanks.

---

<div class="post-metadata">

**Author:** ![nhuynh1](https://avatars.discourse-cdn.com/v4/letter/n/8e7dd6/32.png) [@nhuynh1](https://forum.freecodecamp.org/u/nhuynh1)\
**Post date:** [April 10, 2018, 10:48pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/4 "2018-04-10T22:48:32Z")

</div>

I was confused at first too, but breaking it down everything makes sense.

See fiddle with detailed explanation: [https://jsfiddle.net/nh\_codes/tyck2j64/](https://jsfiddle.net/nh_codes/tyck2j64/)

---

<div class="post-metadata">

**Author:** ![d3eiholo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/d3eiholo/32/102055_2.png) [@d3eiholo](https://forum.freecodecamp.org/u/d3eiholo)\
**Post date:** [July 24, 2018, 6:51pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/5 "2018-07-24T18:51:07Z")

</div>

```auto
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;
}

```

[understand what the logical operators are doing.](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators)

```auto
const [a, b, c] = lines[i];

```

lets us only look at winning combination lines while we start comparing Square’s values.  
Square’s values can only be `'X'` , `'O'` , or `null`.  
if the value can be converted to `false` (if the value is `null`) the `if` statement returns `false`.

```auto
if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
  return squares[a];
}

```

`&&` is checking for `null` values;  
( why is `&&` used twice `¯\_(ツ)_/¯` );  
`===` is comparing the values of `[a]` , `[b]` , and `[c]` to see if they are all `'X'`'s or all `'O'`'s;  
`return squares[a]` can only be `'X'` or `'O'` after passing the `if` statement.

---

<div class="post-metadata">

**Author:** ![Glenlexry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/glenlexry/32/98021_2.png) [@Glenlexry](https://forum.freecodecamp.org/u/Glenlexry)\
**Post date:** [August 23, 2018, 3:01am UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/6 "2018-08-23T03:01:48Z")

</div>

but what i don’t understand is why in this line

if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {

why square[a] has to be by itself. isn’t

if (squares[a] === squares[b] && squares[a] === squares[c]) {

sufficient?

---

<div class="post-metadata">

**Author:** ![Glenlexry](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/glenlexry/32/98021_2.png) [@Glenlexry](https://forum.freecodecamp.org/u/Glenlexry)\
**Post date:** [August 23, 2018, 6:23am UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/8 "2018-08-23T06:23:23Z")

</div>

but isn’t null a falsy value?

---

<div class="post-metadata">

**Author:** ![rabowlen](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rabowlen/32/159217_2.png) [@rabowlen](https://forum.freecodecamp.org/u/rabowlen)\
**Post date:** [January 29, 2019, 1:01am UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/10 "2019-01-29T01:01:20Z")

</div>

Yeah, I basically read that in my head as “are we even dealing with a real ‘a’ here to begin with?”

If not, then there is not point to the rest of it!

---

<div class="post-metadata">

**Author:** ![AFSG](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@AFSG](https://forum.freecodecamp.org/u/AFSG)\
**Post date:** [August 13, 2019, 4:24pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/11 "2019-08-13T16:24:45Z")

</div>

–ok i got it all wrong

---

<div class="post-metadata">

**Author:** ![AFSG](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@AFSG](https://forum.freecodecamp.org/u/AFSG)\
**Post date:** [August 13, 2019, 8:59pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/13 "2019-08-13T20:59:42Z")

</div>

I was confused, i have it right now i guess :slight\_smile, rereading it your explanation makes total sense.  
The first a is needed, we have to test the truthness of [a] first because null==null is returning true.  
Thank you!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 25, 2021, 12:21pm UTC](https://forum.freecodecamp.org/t/need-help-understanding-react-tic-tac-toe-winner-function/137840/15 "2021-01-25T12:21:01Z")

</div>

Closed on January 25, 2021, 12:21pm UTC


