Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

292
Vistas
How to use a while loop to prompt user until they enter 1 of 4 inputs

I'm stuck on how to get this while loop to function properly. I want the program to prompt the user with a question 'Rock, paper, or scissors?' to which they can correctly enter 'rock', 'paper', 'scissors', or 'bomb' (cheat code for auto-win... lol). If they type in anything other than those 4 acceptable inputs, an alert should appear. Then they should be prompted with the same question again. This should repeat until they enter one of those 4 acceptable inputs.

So far, it works if they type in their choice correctly the first time the prompt appears. However, if they type something other than the above 4 acceptable inputs, triggering the alert and the prompt in the while loop, the program does not run correctly even if they enter one of the 4 inputs the next time they are prompted. It gets stuck alternating between the alert and the prompt for eternity.

So how can I get this while loop to work?

function playGame () {
    var input = prompt ('Rock, paper, or scissors?').toLowerCase();
    // testing 123
    // console.log (input);
    var jkl = 0;
    if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
        jkl++
    };
    while (jkl === 0) {
        alert ('ErRoR eRrOr 3rR0r!!!!11');
        prompt ('Rock, paper, or scissors?').toLowerCase();
        if (input === 'rock' || input === 'paper' || input === 'scissors' || input === 'bomb') {
            jkl++
        };
    }
    console.log (input);
    var userChoice = getUserChoice (input);
    var computerChoice = getComputerChoice ();
    console.log (`You: ${userChoice}`);
    console.log (`Computer: ${computerChoice}`);
    console.log (determineWinner (userChoice, computerChoice));
};

playGame ();

After the attempted while loop there is more code that pertains to functions appearing earlier in the program. Ignore that I suppose. Unless anyone thinks it's relevant, in which case I can post that too. Just didn't want to paste a massive wall of code in here.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to update the input variable in the loop.

const options = ['rock', 'paper', 'scissors', 'bomb']
const getOptionPrompt = () => prompt('Rock, paper, or scissors?').toLowerCase();
const checkOption = (input, options) => options.includes(input)

function playGame() {
  let input = getOptionPrompt();
  // testing 123
  // console.log (input);
  var jkl = 0;
  if (checkOption(input, options)) {
    jkl++
  };
  while (jkl === 0) {
    alert('ErRoR eRrOr 3rR0r!!!!11');
    input = getOptionPrompt();
    if (checkOption(input, options)) {
      jkl++
    };
  }
  console.log(input);
  // the functions after this are not defined in the example
  /*
  var userChoice = getUserChoice(input);
  var computerChoice = getComputerChoice();
  console.log(`You: ${userChoice}`);
  console.log(`Computer: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
  */
};

playGame();

But I would definitely update the logic a bit:

const options = ['rock', 'paper', 'scissors', 'bomb']
const getOptionPrompt = () => prompt('Rock, paper, or scissors?')?.toLowerCase() || null;
const checkOption = (input, options) => options.includes(input)

function playGame(options) {
  const input = getOptionPrompt();

  if (!checkOption(input, options) && input != null) {
    alert('ErRoR eRrOr 3rR0r!!!!11');
    playGame(options)
  } else if (input == null) {
    console.log("game canceled")
  } else {
    console.log("result:", input)
  }
};

playGame(options);

This way you don't need the jkl variable, no need for the while loop - if an answer is typed in that is not acceptable, then an error alert shows & the game starts anew.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can change this line

prompt ('Rock, paper, or scissors?').toLowerCase();

to

input = prompt ('Rock, paper, or scissors?').toLowerCase();

Your code is checking the input variable without updating it.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda