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

312
Views
compare function not working in a rock paper scissor programm

i mad a rockpaperscissors program (the code below) that logs user input, cmoputer choice and then compares to get the result. the comparing part doesn't work for some reason it always logs "It's a tie", can someone help.

the code:


  let playerSelection = choice();
  function choice(){
    let input = prompt("pick either rock,paper or scissors");
    console.log(input.toLowerCase());
  }

  let computerChoice = computerPlay();
  function computerPlay(){
    let picks =["rock","paper","scissors"];
    let pick = picks[Math.floor(Math.random(picks)*picks.length)];
    console.log (pick);
  }

  function compare(playerSelection, computerChoice){
    if(playerSelection === computerChoice){
      console.log("It's a tie!")
    }
    else if((playerSelection == "rock" && computerChoice == "scissors")||
       (playerSelection == "paper" && computerChoice == "rock")||
       (playerSelection == "scissors" && computerChoice == "paper")){
      console.log("you win! computer lose.");
    }
    else{
      console.log("computer win! you lose.");
    }
  }
  compare();
}

function game(){
  console.log(playRound());
}

game();```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should pass playerSelection and computerChoice to the compare function as parameters when you call the function:

compare(playerSelection, computerChoice);

Also you should return values from the choice()and computerPlay()function in order to have values assigned to player and computer. Example for computerPlay:

 function computerPlay(){
    let picks =["rock","paper","scissors"];
    let pick = picks[Math.floor(Math.random(picks)*picks.length)];
    return pick;
 }

Here is a full snippet:

  let playerSelection = choice();

  function choice(){
    let input = prompt("pick either rock,paper or scissors");
    return input;
  }

  let computerChoice = computerPlay();

  function computerPlay(){
    let picks =["rock","paper","scissors"];
    let pick = picks[Math.floor(Math.random(picks)*picks.length)];
    return pick;
  }

  function compare(playerSelection, computerChoice){
    if(playerSelection === computerChoice){
      console.log("It's a tie!")
    }
    else if((playerSelection == "rock" && computerChoice == "scissors")||
       (playerSelection == "paper" && computerChoice == "rock")||
       (playerSelection == "scissors" && computerChoice == "paper")){
      console.log("you win! computer lose.");
    }
    else{
      console.log("computer win! you lose. ");
    }
  }
  compare(playerSelection, computerChoice);

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!