Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
Javascript: ¿cómo evito que una función llame a otra función si no se cumple ninguna de las condiciones?

Estoy creando un juego de tres en raya para la clase. En este momento, cuando el usuario hace su movimiento, la función de giro de la computadora está activada, pero estoy tratando de evitar que la "computadora" haga un movimiento cuando el usuario cumple con los criterios ganadores. ¿Me podría ayudar? Gracias

Así funciona mi sistema de turnos

 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(); }

Al principio llamé a la función computerTurn cuando el usuario hacía clic en el tablero, pero ahora estoy tratando de vincularla con la lógica ganadora del juego. Pero dado que mis sentencias if están dentro de forEach, realmente no puedo llamar a la función computerTurn desde allí. También probé con una reasignación de variables, pero creo que no funcionará debido al alcance de forEach.

Gracias de nuevo

Esta es la lógica ganadora.

 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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!