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

336
Visualizações
Getting Rock, Paper, Scissors?

'm working on a simple rock paper scissors script for the Odin Project as part of the course; every answer I get is towards when I run it in the console is ("Draw") no matter what I put in; what am I doing wrong?

Here is the link to the assignment - I know if I took out the various functions I could most likely make it work, but the brief is to use these specific functions hence the issue.

I think the issue might lie in the playerSelection function, but I can't be sure? Would appreciate a little guidance.

Odin Project link: https://www.theodinproject.com/lessons/foundations-rock-paper-scissors

let choice = ["Rock", "Paper", "Scissors"];

function computerPlay() {
  let choice = ["Rock", "Paper", "Scissors"];
  return randomChoice = choice[Math.floor(Math.random() * choice.length)];
}

function playerSelection() {
  let playerChoice = prompt("Enter Rock or Paper or Scissors");
}

function playRound(computerPlay, playerSelection) {
  if (computerPlay === "Rock" && playerSelection === "Scissors") {
    return "Computer wins";
  } else if (computerPlay === "Paper" && playerSelection === "Rock") {
    return "Player wins";
  } else if (computerPlay === "Scissors" && playerSelection === "Paper") {
    return "Computer wins";
  } else if (playerSelection === "Rock" && computerPlay === "Scissors") {
    return "Computer wins";
  } else if (playerSelection === "Paper" && computerPlay === "Rock") {
    return "Player wins";
  } else if (playerSelection === "Scissors" && computerPlay === "Paper") {
    return "Player wins";
  } else {
    return "Draw";
  }
}

playerSelection()
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

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

0

Your have some bugs:

  1. You don't call playerSelection function in console.log()
  2. You don't return value from playerSelection() function
  3. You have two arguments in playRound function (1: computerPlay, 2: playerSelection) but you call playRound with messed up order of arguments.
  4. You have extra call for playerSelection function()

//let choice = ["Rock", "Paper", "Scissors"];

    function computerPlay() {
        let choice = ["Rock", "Paper", "Scissors"];
        return randomChoice = choice[Math.floor(Math.random() * choice.length)];
    }

    function playerSelection() {
        // 2) add return
        return prompt("Enter Rock or Paper or Scissors");
    }

    // 3) change order for arguments
    function playRound(playerSelection, computerPlay) {
        console.log(`Computer choose ${computerPlay}`);
        if (computerPlay === "Rock" && playerSelection === "Scissors") {
            return "Computer wins";
        } else if (computerPlay === "Paper" && playerSelection === "Rock") {
            return "Player wins";
        } else if (computerPlay === "Scissors" && playerSelection === "Paper") {
            return "Computer wins";
        } else if (playerSelection === "Rock" && computerPlay === "Scissors") {
            return "Computer wins";
        } else if (playerSelection === "Paper" && computerPlay === "Rock") {
            return "Player wins";
        } else if (playerSelection === "Scissors" && computerPlay === "Paper") {
            return "Player wins";
        } else {
            return "Draw";
        }
    }

    // 4) remove playerSelection call
    // playerSelection()
    const computerSelection = computerPlay();
    // 1) add call to playerSelection() function
    console.log(playRound(playerSelection(), computerSelection));

P.s. you have to use object to find winner:

function computerPlay() {
        let choice = ["Rock", "Paper", "Scissors"];
        return randomChoice = choice[Math.floor(Math.random() * choice.length)];
    }

    function playerSelection() {
        return prompt("Enter Rock or Paper or Scissors");
    }

    function playRound(playerSelection, computerPlay) {
        console.log(`player: ${playerSelection} VS  + computer: ${computerPlay}`);
        if (playerSelection === computerPlay) return 'Draw'
        const winPairs = {
            Paper: 'Rock',
            Scissors: 'Paper',
            Rock: 'Scissors'
        }
        return winPairs[playerSelection] === computerPlay ? 'Player wins' : 'Computer wins'
    }
    
    console.log(playRound(playerSelection(), computerPlay()));

about 4 years ago · Juan Pablo Isaza Relatório

0

Like the others wrote, your prompt never "lands" in the if chain, so that every time the last else statement is called.

The easiest way would be, to omit the other two functions and insert their two neccesary lines in the function playRound().

Working example:

const choice = ["Rock", "Paper", "Scissors"];

function playRound() {
  const computerSelection = choice[Math.floor(Math.random() * choice.length)];
  const playerSelection = prompt("Enter Rock or Paper or Scissors");
  
  if (computerSelection === "Rock" && playerSelection === "Scissors") {
    return "Computer wins";
  } else if (computerSelection === "Paper" && playerSelection === "Rock") {
    return "Player wins";
  } else if (computerSelection === "Scissors" && playerSelection === "Paper") {
    return "Computer wins";
  } else if (playerSelection === "Rock" && computerSelection === "Scissors") {
    return "Computer wins";
  } else if (playerSelection === "Paper" && computerSelection === "Rock") {
    return "Player wins";
  } else if (playerSelection === "Scissors" && computerSelection === "Paper") {
    return "Player wins";
  } else {
    return "Draw";
  }
}

console.log(playRound());

about 4 years ago · Juan Pablo Isaza Relatório

0

The playerSelection function is not returning the selection but just declaring a variable with the output of the prompt function as its output. You should return the variable like this:

function playerSelection() {
   let playerChoice = prompt("Enter Rock or Paper or Scissors"); 
   return playerChoice
}

This is why it is returning "Draw"

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