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

278
Visualizações
Javascript - How do I prevent a function from calling another function if none of the conditions are met?

I'm creating a tic tac toe game for class. Right now when the user makes their move, the computer turn function is activated, but I'm trying to stop the "computer" from making a move when the user meets the winning criteria. Could you please help me? Thanks

This is how my turn system works

function leaveMark(){
  let toes = document.querySelectorAll('img');
  let toesId = this.getAttribute('data-id');
  chosenToeId.push(toesId);
  takenId.push(toesId);
  userChoices.push(toesId);
  let chosenToe = chosenToeId[0];
  toes[chosenToe].setAttribute('src','img/tic-tac-toe/hammer.png');
  chosenToeId = [];
  userValidation();
  // setTimeout(computerTurn, 1000);
}

/////computer turn////////

function computerTurn(){
  let randomId = Math.floor((Math.random()*imgId.length));
  let randomIdString = randomId.toString();
  let toes = document.querySelectorAll('img');
  if (!takenId.includes(randomIdString)){
    toes[randomId].setAttribute('src','img/tic-tac-toe/boots.png');
    takenId.push(randomIdString);
    computerChoices.push(randomId);
  } else{
      computerTurn();
    }
    computerValidation();
  }

At first I called the computerTurn function when the user clicked on the board, but now I'm trying to link it to the winning logic of the game. But since my if statements are inside a forEach, I can't really call the computerTurn function from there. I also tried with a reassignment of variables but I think it won't work because of the scope of the forEach.

Thank you again

This is the winning logic.

function userValidation() {
  let nextTurn = false;
  let numberUser = userChoices.map(i=>Number(i));
  winningCombos.forEach((e) =>{
    let a = e[0];
    let b = e[1];
    let c = e[2];
    if (numberUser.includes(a) && numberUser.includes(b) && numberUser.includes(c)){ 
      console.log('user wins');
      
    } else if (takenId.length === imgId.length){
      console.log('This is a tie. Not bad, try again.');
      
    } else {
      nextTurn = true;
      
     
    }
  })
  if (nextTurn == true){
    setTimeout(computerTurn, 1000)
  }
}

  function computerValidation() {
    let numberComputer = computerChoices.map(i=>Number(i));
    winningCombos.forEach((e) =>{
      let a = e[0];
      let b = e[1];
      let c = e[2];
      if (numberComputer.includes(a) && numberComputer.includes(b) && numberComputer.includes(c)){
        console.log('Sorry, you lost. Try again!');
      } else if (takenId.length === imgId.length){
        console.log('This is a tie. Not bad, try again.');
      } else {
        messages.textContent = 'Your turn, user.'
      }
    });
  };
about 4 years ago · Juan Pablo Isaza
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