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

166
Views
Variables que cambian cuando no deberían

tengo este codigo:

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <form> <input placeholder="GUEEEEEEEEEEEEEEEEEEEES" name="guess" id="guess"/> <input type="submit" onclick="check()" value="EEEE"/> </form> <h1 id="score"></h1> </body> <script> var answer = Math.floor(Math.random()*100); var score = 0; function check(){ var guess = document.getElementById("guess").value; var guessInt = parseInt(guess); if (parseInt(guess) == answer) { alert("you got it!") } else if (parseInt(guess) > answer) { alert("too high") } else if (parseInt(guess) < answer) { alert("too low") } } document.getElementById("score").innerHTML = score; // < display score </script>
y en el javascript, defino la respuesta de la variable, pero cada vez que se ejecuta check(), la variable obtiene un nuevo valor, cuando adivino, obtengo un valor demasiado alto o demasiado bajo o lo recibió alerta y sé que las variables se vuelven nuevas valores cada vez que introduzco una nueva conjetura porque verifico cuál es la variable (usando un punto de interrupción para hacer una pausa) y luego introduzco la respuesta correcta. Vuelvo a ejecutar el código e introduzco la respuesta correcta y luego lo reviso de nuevo y es diferente.
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Obtiene un nuevo valor, porque no está evitando que se envíe el formulario, por lo tanto, está recargando la página después de cada clic en el botón. Intente cambiar la entrada que tiene el detector de clics para verificar, a un tipo de botón de no envío, es decir:

En lugar de <input type="submit" onclick="check()" value="EEEE"/> , utilice <button type='button' onclick='check()'>Submit</button>

about 4 years ago · Santiago Trujillo Report

0

Intente usar event.preventDefault() para evitar que el formulario vuelva a cargar la página cada vez que se envía.

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <form> <input placeholder="GUEEEEEEEEEEEEEEEEEEEES" name="guess" id="guess"/> <input type="submit" onclick="check(event)" value="EEEE"/> </form> <h1 id="score"></h1> </body> <script> var answer = Math.floor(Math.random()*100); var score = 0; function check(event){ event.preventDefault() var guess = document.getElementById("guess").value; var guessInt = parseInt(guess); if (parseInt(guess) == answer) { alert("you got it!") } else if (parseInt(guess) > answer) { alert("too high") } else if (parseInt(guess) < answer) { alert("too low") } } document.getElementById("score").innerHTML = score; // < display score </script>

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!