I have an If-Else block what is really big, and ugly in my opinion.    if(fields[a1] == x && fields[a2] == x){        place(a3);    } else if (fields[a2] == x && fields[a3] == x){        place(a1);    } else if (fields[a1] == x && fields[a3] == x){        place(a2);    } else if (fields[b1] == x && fields[b2] == x){        place(b3);    } else if (fields[b2] == x && fields[b3] == x){        place(b1);    }else if (fields[b1] == x && fields[b3] == x){        place(b2);    }This is just a little part of it. The real if-else blocks is 4 times as big. How can I compact this down to just something like a loop so I can loop trough it. For each if statement I have to do a similar task. I've thought about having an array with some values and then looping trough it, but I don't really know how to do that without making it worse.
Javascript - Better way of handling big if else blocks
javascript
It makes things easier if you define fields as a two dimensional array like thislet fields = [  [, , ],  [, , ],  [, , ]];Then you can write a function to place the next symbol (x or o) on the field. e.g.function placeNext(symbol) {  for(let row = 0; row < 3; row++) {    if(fields[row][0] == symbol && fields[row][1] == symbol) {      place(row, 2, symbol); return;    }    if(fields[row][0] == symbol && fields[row][2] == symbol) {      place(row, 1, symbol); return;    }    if(fields[row][1] == symbol && fields[row][2] == symbol) {      place(row, 0, symbol); return;    }  }  for(let column = 0; column < 3; column++) {    if(fields[0][column] == symbol && fields[1][column] == symbol) {      place(2, column, symbol); return;    }    if(fields[0][column] == symbol && fields[2][column] == symbol) {      place(1, column, symbol); return;    }    if(fields[1][column] == symbol && fields[2][column] == symbol) {      place(0, column, symbol); return;    }  }}The diagonals are still missing in this code though.