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

283
Visualizações
How do I close a prompt upon clicking cancel in JS?

I cannot for the life of me figure out how to close the prompt altogether upon pressing cancel in my code for a guessing game. I've tried several things, but each time either the code ignores what I'm trying to do, or ends in an error. Here's the code in question:

let numGuess = prompt (`Guess the number I am thinking of (between 1 and 10):`);
let num = (4);

let numTries = 1

console.log(numGuess);

numGuess = parseInt(numGuess)

if (prompt == null) {

alert (`Press F5 to play again.`) 


}
if (numGuess === num) {

  alert (`Correct! You guessed the number in 1 try! Press F5 to play again.`);

}

else while ((numGuess !== num) && (numTries <3))

{

  numTries = numTries++

  alert (`Incorrect guess. Try again.`)

}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Check for the user input value, not for prompt. And also don't use while loop here .Loop is used for the traverse.

let numGuess = prompt (`Guess the number I am thinking of (between 1 and 10):`);
let num = (4);

let numTries = 1

console.log(numGuess);

numGuess = parseInt(numGuess)

if (!numGuess) {

alert (`Press F5 to play again.`) 


}
else if (numGuess === num) {

  alert (`Correct! You guessed the number in 1 try! Press F5 to play again.`);

}

else{
  numTries = numTries++

  alert (`Incorrect guess. Try again.`)

}

about 4 years ago · Juan Pablo Isaza Relatório

0

The main issue with your code is using "++" after numTries which make the numTries variable value doesn't change and the while loop continues working without stopping, you need to insert it before. ex:

++numTries

The code below is a working example of what you want to do.

let numGuess;
let numTries = 0;
let num = 4;
const guessFunction = () => {
    if (numTries >= 3) {
        alert("Sorry you Failed in the 3 tries! Press F5 to play again.");
        return;
    }
    numGuess = parseInt(
        prompt(`Guess the number I am thinking of (between 1 and 10):`)
    );
    numTries = ++numTries
    if (numGuess === num) {
        alert(`Correct! You guessed the number in ${numTries} try! Press F5 to play again.`);
        return;
    }
    if(numTries <= 2) {
        alert(`Incorrect guess. Try again.`);
    }
    guessFunction()
};

guessFunction()

about 4 years ago · Juan Pablo Isaza Relatório
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