0

I'm working on a tic-tac-toe game. Trying to pass in the name of an enclosed variable to select and a value to assign to it. However, this isn't working...if I pass in s1 without quotes it creates an error because s1 isn't a global variable, and if I pass it in with quotes, it runs, but seems to create a new variable called "square" and not use the value of the square argument at all.

So, I could imagine doing this by looping through all the variables and comparing each to my argument string, but is there a better way to do this?

const gameBoard = ((squareStatus) => {
  const squares = document.querySelectorAll('.square');
  for (let i = 0; i < 9; i++) {
    console.log(squareStatus[i])
    //squares[i].innerHTML = squareStatus[i];
  }
});

const gameBrain = function(square, mark) {
  const s1 = "x";
  const s2 = "x";
  const s3 = "o";
  const s4 = "x";
  const s5 = "o";
  const s6 = "o";
  const s7 = "o";
  const s8 = "x";
  const s9 = "x";
  square = mark;
  gameBoard([s1, s2, s3, s4, s5, s6, s7, s8, s9]);
}

gameBrain('s1', 'z');
brk
  • 48,835
  • 10
  • 56
  • 78
Laura Gyre
  • 141
  • 8
  • 1
    Please show `gameBoard` – brk May 05 '20 at 16:51
  • This is gameBoard, which seems to be working fine: ``` const gameBoard = ((squareStatus) => { console.log(squareStatus); const squares = document.querySelectorAll('.square'); for (let i = 0; i < 9; i++) { squares[i].innerHTML = squareStatus[i]; } }); ``` – Laura Gyre May 05 '20 at 16:54
  • No, *don't* pass the name of a variable. Keep those in an object, and pass the name of the property. – VLAZ May 05 '20 at 16:54
  • Can you explain what you are trying to do in `square=mark`? – Yosef Tukachinsky May 05 '20 at 16:57
  • @LauraGyre i edited your code and added stack snippet but could not find any error – brk May 05 '20 at 17:00

0 Answers0