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

193
Views
For loop returns at only the last iteration javascript

I have created a loop to run a function 5 times.

When I run the code it prompts the message 5 times, as requested, but it only displays the result on the last iteration.

This is what the code looks like:

        let sign = prompt("Rock, paper or scissors?");
       
        function playRound(playerSelection, computerSelection) {
            if (playerSelection == "rock" && computerSelection == choices[0]) {
                return "It's a tie! Play again!";
            } else if (playerSelection  == "rock" && computerSelection == choices[1]) {
                return "You lose! Paper beats rock.";
            } else if (playerSelection  == "rock" && computerSelection == choices[2]) {
                return "You win! Rock beats scissors."; 
            } else if (playerSelection  == "paper" && computerSelection == choices[0]) {
                return "You win! Paper beats rock.";
            } else if (playerSelection  == "paper" && computerSelection == choices[1]) {
                return "It's a tie! Play again!";
            } else if (playerSelection  == "paper" && computerSelection == choices[2]) {
                return "You lose! Scissors beat paper.";
            } else if (playerSelection  == "scissors" && computerSelection == choices[0]) {
                return "You lose! Rock beats scissors.";
            } else if (playerSelection  == "scissors" && computerSelection == choices[1]) {
                return "You win! Scissors beat paper.";
            } else if (playerSelection  == "scissors" && computerSelection == choices[2]) {
                return "It's a tie! Play again!";
            } else return "I think you forgot how to play this game!"; 

        }

        let playerSelection = sign.toLowerCase();
        let computerSelection = randomValue();

        para.innerHTML = sign.toLowerCase();
        out2.innerHTML= playRound(playerSelection, computerSelection); 

    }```


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