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

131
Views
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 answers
Answer question

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 Report

0

Well you are not calling the function. use

game();

after function definition

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