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

280
Views
¿Hay algún error al comparar cadenas en JS?

Estoy haciendo un juego de tres en raya y tengo un problema. Si la entrada del usuario no es igual a X u O , entonces debería imprimir el mensaje Ingrese los signos correctos, pero no tengo idea de qué está mal en mi código. Solo si el bloque no funciona correctamente.

aquí está el código de esa función:

 let p1, p2, s1, s2; function startGame() { playAgain(); p1 = document.getElementById("p1").value; p2 = document.getElementById("p2").value; s1 = document.getElementById("s1").value; s2 = document.getElementById("s2").value; if (p1 == "" || p2 == "" || s1 == "" || s2 == "") { alert("Enter the details."); playAgain(); } else if ( s1 != "X" || s1 != "x" || s1 != "O" || s1 != "o" || s2 != "X" || s2 != "x" || s2 != "O" || s2 != "o" ) { alert("Enter correct signs."); playAgain(); } else { alert("You can start the game." + p1 + s1 + p2 + s2); isStarted = true; } }

gracias

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Trate de "y" sus condiciones en lugar de "orearlas".

 else if ( (s1 != "X" && s1 != "x" && s1 != "O" && s1 != "o") || (s2 != "X" && s2 != "x" && s2 != "O" && s2 != "o") )

Yo personalmente haría uso de every las funciones para hacerlo aún más corto y menos repetitivo:

 const validInputs = ["X", "x", "O", "o"] else if (validInputs.every((item) => s1 !== item) || validInputs.every((item) => s2 !== item)) { ...
about 4 years ago · Santiago Trujillo Report

0

Parece que hay un error lógico en la declaración if, probé esto y funciona correctamente

 if ( s1 != "X" && s1 != "x" && s1 != "O" && s1 != "o" || s2 != "X" && s2 != "x" && s2 != "O" && s2 != "o" ) {

Pero también necesita validar que s1 no es igual a s2 porque los usuarios pueden escribir algo como s1='X', s2='x' y sería válido

about 4 years ago · Santiago Trujillo Report
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!