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

259
Visualizações
How do I put logical operations in a while loop in Javascript?

I want to create a guessing game where you pick a maximum number in the prompt and you type the number that the program randomly generates.

However, I want it so that when you press "q", the game quits automatically.

Here's my code

let maximum = parseInt(prompt("Enter the maximum number"));

  
while (!maximum) {
  maximum = parseInt(prompt("Please Enter a valid number!"));
}


let targetNum = Math.floor(Math.random() * maximum + 1);
console.log(targetNum)

let guess = parseInt(prompt("Enter your first guess!"));
let attempts = 1;

while(parseInt(guess) !== targetNum  || guess.toString !== "q") {
 if (guess > targetNum) {
   guess = prompt("too high! Enter a new guess")
 }
 else {
  guess = prompt("too low! Enter a new guess")
  attempts++;
}
}
console.log(`You got it! It took you ${attempts} guesses`)

Whenever I press q, it doesn't work. Can someone please explain to me why :) ?

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

0

I put your variables on top to make it more clean. I also added an else if that breaks the loop when i type "q".

let maximum = parseInt(prompt("Enter the maximum number"));
let guess = parseInt(prompt("Enter your first guess!"));
let attempts = 1;
let targetNum = Math.floor(Math.random() * maximum + 1);


while (!maximum) {
   maximum = parseInt(prompt("Please Enter a valid number!"));
}
while(parseInt(guess) !== targetNum) {
   if (guess > targetNum) {
      guess = prompt("too high! Enter a new guess")
      attempts++;
   }
   else if(guess < targetNum){
      guess = prompt("too low! Enter a new guess")
      attempts++;
   }else if(guess == "q"){
      break;
   }
}
console.log(targetNum)
console.log(`You got it! It took you ${attempts} guesses`)
about 4 years ago · Juan Pablo Isaza Relatório

0

const guessNumberGame = {
  start_  : function(){
    let n = prompt( 'Enter the secret number:' );
    if( n ){
      if ( !isNaN( n ) ){
        this.guess_(n);
      }else{
        alert( 'That is not a valid number!' );
        this.start_();
      }
    }else{
      alert( 'Cancel the game!' );
    }
  },
  guess_ : function(n){
    const g = prompt( 'guess the number!' );
    if( g ){
      if ( !isNaN( g ) ){
        if( g > n ){
          alert( 'Too high!' );
          this.guess_(n);
        }
        else if( g < n ){
          alert( 'Too low!' );
          this.guess_(n);
        }
        else{
          alert( 'Correct!' );
          this.start_();
        }
      }else{
        alert( 'That is not a valid number!' );
        this.guess_(n);
      }
    }else{
      this.start_();
    }
  }
};

guessNumberGame.start_();

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