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

137
Vistas
JavaScript - Prompt inside function doesn't appear on screen, but does outside the function

I am building a rock, paper, scissors project which is supposed to take 5 rounds. The user will get a prompt and input either "rock", "paper", or "scissors". When I write the code like this:

let playerSelection = prompt("Rock, paper ou scissors?").toLowerCase();


let game = function() {
  for (let i = 0; i < 5; i++) {

    compare(playerSelection, computerPlay);
      if (compare === "You win!") {
       playerScore++;
      }
      else if (compare === "You lose!") {
        computerScore++;
      }
      else if (compare === "It's a tie!") {
  
      }

 }
}

The prompt is prompted and I get to input something, however it only happens once and the loop doesn't work. When I put the prompt inside the loop, like this:

let game = function() {
  for (let i = 0; i < 5; i++) {
    
    let playerSelection = prompt("Rock, paper ou scissors?").toLowerCase();

    compare(playerSelection, computerPlay);
      if (compare === "You win!") {
       playerScore++;
      }
      else if (compare === "You lose!") {
        computerScore++;
      }
      else if (compare === "It's a tie!") {
  
      }

 }
}

The prompt never shows up! How can I solve this and make it prompt 5 times?

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

0

I think there is some error in your compare function. checkout the snippet below its working fine.

const compare = function(playerSelection, computerPlay) {

  // write your logic here
  return "You win!";
}
let game = function() {
  for (let i = 0; i < 5; i++) {
    
    let playerSelection = prompt("Rock, paper ou scissors?").toLowerCase();
    const outputs = ["rock", "paper", "scissor"];
    compare(playerSelection, Math.floor(Math.random()*3));
      if (compare === "You win!") {
       playerScore++;
      }
      else if (compare === "You lose!") {
        computerScore++;
      }
      else if (compare === "It's a tie!") {
  
      }

 }
}

game();

about 4 years ago · Juan Pablo Isaza Denunciar

0

Well you are not calling the function. use

game();

after function definition

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