Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
I am making a rock, paper, scissors game and the values of the outputs are not all corect

I typed the statements and everything is working fine only when I try the console with the answer of rock. When I try to see other logs(paper or scissors) it gives me the output based on the first 3 if conditions(from the rock) and the others for some reason doesn't apply.

//My "choice"// 
function playerPlay() {
  let Answer = prompt(`Do you choose Rock,Paper or Scissors ?`).toLowerCase();
  console.log(Answer);
}
playerPlay()
//Computer random choice//
function computerPlay() {
  computerPlay = Math.floor(Math.random() * 3)
  if (computerPlay === 0) {
    console.log("rock");
  } else if (computerPlay === 1) {
    console.log("paper");
  } else if (computerPlay === 2) {
    console.log("scissors");
  }
}
computerPlay()

//The conditions for the game//

function rules() {
  if (Answer = "rock" && computerPlay === 0) {
    return ("Draw");
  } else if (Answer = "rock" && computerPlay === 1) {
    return ("You lost");
  } else if (Answer = "rock" && computerPlay === 2) {
    return ("You won");
  }

  if (Answer = "paper" && computerPlay1 === 0) {
    return ("You Won!");
  } else if (Answer = "paper" && computerPlay === 1) {
    return ("Draw");
  } else if (Answer = "paper" && computerPlay === 2) {
    return ("You Lost!");

  }
  if (Answer = "scissors" && computerPlay === 0) {
    return ("You Lost!")
  } else if (Answer = "scissors" && computerPlay === 1) {
    return ("You Won!");

  } else if (Answer = "scissors" && computerPlay === 2) {
    return ("Draw!");

  }
}
rules()

In the Image you can see that paper with paper is supposed to be a tie but the output actually is "You lost!"

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

answer = "..." should be answer == "..." in your if statements. = is used for assignment not for checking equality

You are defining the answer variable inside of your function using let. This means that the answer is only defined within the scope of the function and is not a global variable. Instead you should have it return answer, and instead of calling playerPlay() you should do answer = playerPlay()

The same issue exists with your computer play function. Your rules function returns the answer but nothing prints it out.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda