Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!