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

218
Views
JavaScript - preguntas para principiantes

Tengo algunas dificultades con mi primer número para adivinar "juego" en JavaScript. ¿Alguien puede echar un vistazo y orientarme en lo que hice mal? Empecé con ese idioma no hace mucho tiempo...

La función se asigna a un botón

 <input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start !

El usuario (jugador) debe presionar el botón, adivinar el número (1-10) en 3 intentos y luego jugar de nuevo o no. Cada intento que hago dice "el número es mayor", pero al final el resultado es aleatorio, incluso si elige 10.

 var hiddenNum; var attemps; hiddenNum = Math.floor(Math.random() * 10); hiddenNum = hiddenNum + 1; attemps = 0; function check(guess) { window.prompt("Please enter the number between 1 and 10", "10"); if (hiddenNum == guess) { window.alert("Congratulations! You guessed correctly !"); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } else { attemps = attemps + 1; if (hiddenNum < guess) { result = "lower"; } else { result = "higher"; } window.alert("Guess number " + attemps + " is incorrect. The number is " + result + "."); } if (attemps >= 3) { window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Diseñó su función de check para que se le pase una suposición, pero solo la está llamando sin hacerlo. Entonces todas sus comparaciones son contra undefined .

También está utilizando window.prompt para obtener la suposición real. Lo que debe hacer es guardar el valor devuelto por el aviso en una variable y luego compararlo. Revisa el fragmento.

 var hiddenNum; var attemps; hiddenNum = Math.floor(Math.random() * 10); hiddenNum = hiddenNum + 1; attemps = 0; function check() { let guess = window.prompt("Please enter the number between 1 and 10", "10"); if (hiddenNum == guess) { window.alert("Congratulations! You guessed correctly !"); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } else { attemps = attemps + 1; if (hiddenNum < guess) { result = "lower"; } else { result = "higher"; } window.alert( "Guess number " + attemps + " is incorrect. The number is " + result + "." ); } if (attemps >= 3) { window.alert( "Sorry, you have run out of guesses! The number was " + hiddenNum ); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } }
 <input id="box;" class="btn" ; type="button" value="Guess" onClick="check()">Click to start

over 4 years ago · Santiago Trujillo Report

0

necesita asignar el indicador

 var hiddenNum; var attemps; hiddenNum = Math.floor(Math.random() * 10); hiddenNum = hiddenNum + 1; attemps = 0; function check() { let guess = window.prompt("Please enter the number between 1 and 10", "10"); if (hiddenNum == guess) { window.alert("Congratulations! You guessed correctly !"); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } else { attemps = attemps + 1; if (hiddenNum < guess) { result = "lower"; } else { result = "higher"; } window.alert("Guess number " + attemps + " is incorrect. The number is " + result + "."); } if (attemps >= 3) { window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum); again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again == "N" || again == "n") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } }
 <input id="box;" class="btn" ; type="button" value='guess'onClick="check()">Click to start! User (player) have to press the button, guess the number (1-10) within 3 attemps and then play again or not. Every attemp I do it says "number is higher" but at the end result is random, even if you chose 10.

over 4 years ago · Santiago Trujillo Report

0

Debe almacenar el valor de window.prompt. En su código, solo usa guess en el parámetro de la función como el valor de window.prompt que es incorrecto.

Sintaxis para window.promp :

resultado = window.prompt(mensaje, predeterminado);

Además, acorté sus códigos y almacené el elemento duplicado en una función para que sea un poco menos complicado.

 function smallcheck() { let again = window.prompt("Would you like to try again? Enter Y or N.", "Y"); if (again.toUpperCase() == "N") { window.alert("Thanks for trying. Goodbye."); window.close(); } else { window.alert("The number has been randomized."); window.location.reload(); } } function check() { let guess = window.prompt("Please enter the number between 1 and 10", "10"); console.log(guess) if (hiddenNum == guess) { window.alert("Congratulations! You guessed correctly !"); smallcheck() } else { attempts++; if (hiddenNum < guess) { result = "lower"; } else { result = "higher"; } window.alert("Guess number " + attempts + " is incorrect. The number is " + result + "."); } if (attempts >= 3) { window.alert("Sorry, you have run out of guesses! The number was " + hiddenNum); smallcheck() } }

over 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!