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

181
Visualizações
What is wrong with my if/else statements?

My first time and first project: Rock, Paper, Scissors. I have spent hours trying to figure out what could be wrong with my if/else statements. Regardless of conditions evaluated, it returns the first statement(It's a tie"). My code is as below;

const computerSelection = computerPlay();
const playerSelection = humanPlay();

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

function humanPlay() {
  let selectItem = prompt(
    "Please pick one battle item: rock, paper, or scissors"
  );
  return selectItem.toLowerCase();
}

function playRound(playerSelection, computerSelection) {
  if (playerSelection === computerSelection) {
    return "It's a tie!";
  } else if (computerSelection === "rock" && playerSelection === "scissors") {
    return "You lose!";
  } else if (computerSelection === "scissors" && playerSelection === "rock") {
    return "You win";
  } else if (computerSelection === "scissors" && playerSelection === "paper") {
    return "You lose";
  } else if (computerSelection === "paper" && playerSelection === "scissors") {
    return "You win";
  } else if (computerSelection === "paper" && playerSelection === "rock") {
    return "You lose";
  } else if (computerSelection === "rock" && playerSelection === "paper") {
    return "You win";
  } else {
    return "Please play again";
  }
}

console.log(computerSelection);
console.log(playerSelection);
console.log(playRound());

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

0

Add console.log(playerSelection, computerSelection) to the first line of the playRound() function. You'll see that both values are undefined, because you aren't passing those values into the function when you call it.

To fix, change the very last line to: console.log(playRound(computerSelection, playerSelection));

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