'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));
Your have some bugs:
//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()));
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());
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"