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

314
Vistas
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 Respuestas
Responde la pregunta

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 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