Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
To get a truthy condition(array) from a nested array, for now the function returns boolean value true

The checkWin function returns true when an array which are index numbers have a speicified symbol on another Array's indexes. How to retrieve the "cond"(array) which results in true from the nested winConditions array ?

Symbols get populated using the click event listener.

The expected result should be, if .some(cond) becomes true then return that cond for eg. if symbol "X" is present on [0, 1, 2] then return this array

let testArray = Array(9).fill("") 
 
  const winConditions = [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8],
    [0, 3, 6],
    [1, 4, 7],
    [2, 5, 8],
    [0, 4, 8],
    [2, 4, 6],
  ];

  let xValue = "X";
  let oValue = "O";

function checkWin(value, array) {
    return winConditions.some((cond) =>
        cond.every((index) => array[index] == value));
}

console.log(checkWin(xValue, testArray));
console.log(checkWin(oValue, testArray));
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use .find() instead of .some(), this will return the first array that the .every() callback returns true for. You can use this array to determine if there was a win or not for a particular player:

const testArray = Array(9).fill("");
testArray[3] = testArray[4] = testArray[5] = "O"; // test winning position
const winConditions = [
  [0, 1, 2],
  [3, 4, 5],
  [6, 7, 8],
  [0, 3, 6],
  [1, 4, 7],
  [2, 5, 8],
  [0, 4, 8],
  [2, 4, 6],
];

const xValue = "X";
const oValue = "O";

function getWinPos(value, array) {
  return winConditions.find((cond) =>
    cond.every((index) => array[index] == value)
  );
}

const xWinPos = getWinPos(xValue, testArray);
const oWinPos = getWinPos(oValue, testArray);

if(xWinPos) { // found a winning position row/col/diag for "X"
  console.log(xWinPos);
} else if(oWinPos) { // found a winning row/col/diag for "O"
  console.log(oWinPos);
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda