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

291
Visualizações
Rock Paper Scissors help (JavaScript)

Currently working on TOP rock paper scissors game and I'm having some trouble. The function playRound is showing up as undefined in the console. I'm not sure why as all my other function are working. Maybe i did something wrong in the other functions but I'm not sure. I feel like I'm so close yet so far from this thing working. Maybe it's just common beginner mistakes not sure. Any help would be appreciated.

function computerPlay() {
    var pickRandom = ["Rock", "Paper", "Scissors"];
    var randomMove = pickRandom[Math.floor(Math.random() * 3)];

    if (randomMove == "Rock") {
        randomMove.value = 0;
    } else if (randomMove == "Paper") {
        randomMove.value = 1;
    } else if (randomMove == "Scissors") {
        randomMove.value = 2;
    }
    return randomMove;
}

console.log(computerPlay());


var playerSelection = prompt("pick rock paper or scissors");

function userPlay() {

    if (playerSelection == "rock") {
        playerSelection.value = 0;
    } else if (playerSelection == "paper") {
        playerSelection.value = 1;
    } else if (playerSelection == "scissors") {
        playerSelection.value = 2;
    }
    return playerSelection;
}

console.log(userPlay());

function playRound(playerPick, computerSelection) {
    if (playerPick == 0 && computerSelection == 2) {
        alert("you win!!!");
    } else if (playerPick == 0 && computerSelection == 1) {
        alert("you lose hahahahaha!!!");
    } else if (playerPick == computerSelection) {
        alert("its a tie");
    }

}

const playerPick = userPlay();
const computerSelection = computerPlay();
console.log(playRound(playerPick, computerSelection));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

function computerPlay() {
  var pickRandom = ["Rock", "Paper", "Scissors"];
  var randomMove = pickRandom[Math.floor(Math.random() * 3)];

  if (randomMove == "Rock") {
    return 0;
  } else if (randomMove == "Paper") {
    return 1;
  } else if (randomMove == "Scissors") {
    return 2;
  }
}



var playerSelection = prompt("pick Rock, Paper or Scissors");

function userPlay() {

  if (playerSelection == "Rock") {
    return 0;
  } else if (playerSelection == "Paper") {
    return 1;
  } else if (playerSelection == "Scissors") {
    return 2;
  }
}


function playRound(playerPick, computerSelection) {
  console.log("player", playerPick)
  console.log("computerSelection", computerSelection)
  if (playerPick == 0 && computerSelection == 2) {
    console.log("you win!!!");
  } else if (playerPick == 0 && computerSelection == 1) {
    console.log("you lose hahahahaha!!!");
  } else if (playerPick == computerSelection) {
    console.log("its a tie");
  }
}

const playerPick = userPlay();
const computerSelection = computerPlay();
playRound(playerPick, computerSelection);

You you just needed to return the numbers from computerPlay and userPlay. Also, be careful with case sensitivity when using the prompt.

about 4 years ago · Juan Pablo Isaza Relatório

0

You shouldn't run it in console.log, as it doesn't have a return keyword, so it will return undefined. Just run it normally and it will be fine.

about 4 years ago · Juan Pablo Isaza Relatório

0

You are close. I cleaned it up a bit, added a function that maps variants of human input to a normalized value, and replaced the (incomplete) conditions in playRound with an object that captures all of the win conditions in an object (see beats)...

function normalizePlay(play) {
  const variants = { r: 'rock', rock: 'rock', p: 'paper', paper: 'paper', s: 'scissors', scissors: 'scissors' };
  return variants [play.toLowerCase()];
}

function computerPlay() {
    var pickRandom = ["rock", "paper", "scissors"];
    return pickRandom[Math.floor(Math.random() * 3)];
}

function playRound(human, computer) {
   const beats = { rock: 'scissors', scissors: 'paper', paper: 'rock' }
    let result;
    if (human === computer) {
      result = 'tie';
    } 
    else if (beats[human] === computer) {
      result = 'you win';
    }
    else result = 'computer wins';
    console.log(`you picked ${human}, computer picked ${computer}... ${result}`)
}

const human = normalizePlay(prompt("pick rock paper or scissors (or r, p, s)"));
const computer = computerPlay();
playRound(human, computer);

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