Getting Rock, Paper, Scissors? - javascript

'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:
You don't call playerSelection function in console.log()
You don't return value from playerSelection() function
You have two arguments in playRound function (1: computerPlay, 2: playerSelection) but you call playRound with messed up order of arguments.
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()));

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"

You have 2 problems.
First, you're calling the playerSelection function at the bottom without getting the value and assigning it to a variable to pass to the playRound function. Change this to:
const playerChoice = playerSelection()
const computerSelection = computerPlay();
console.log(playRound(playerChoice, computerSelection));
Secondly, your playerSelection function is never returning the selected choice, change this to:
function playerSelection() {
let playerChoice = prompt('Enter Rock or Paper or Scissors');
return playerChoice;
}
Demo.

Related

Stuck on Rock, Paper, Scissors

I'm working through my first JS project and making a basic Rock, Paper, Scissors game. I get stuck when I attempt to play a single round. I don't know if my code accepts case-insensitive input from the user (rock, Rock, ROCK, rOcK).
My console seems to display a random result of the game. For example, if I input Rock, and the computer also inputs Rock, the console sometimes displays "You win!", and I don't know why that's happening.
Here is my code below. Any guidance is greatly appreciated!
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3)
switch (randomNum) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
console.log(getComputerChoice());
function userInput() {
const ask = prompt("To begin game, select Rock, Paper, or Scissors");
return ask.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
return "You win!";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You lose";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "You win!";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
return "You lose";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You win!";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
return "You lose";
} else if (playerSelection == computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
const playerSelection = userInput();
const computerSelection = getComputerChoice();
console.log(playRound(playerSelection, computerSelection));
It looks like you are displaying a random computer choice, then selecting again for the game. To fix this remove line 12 (console.log(getComputerChoice());) and add the following line right after you declare computerSelection:
console.log(computerSelection);
Here is the final code:
function getComputerChoice() {
const randomNum = Math.floor(Math.random() * 3)
switch (randomNum) {
case 0:
return "rock";
case 1:
return "paper";
case 2:
return "scissors";
}
}
function userInput() {
const ask = prompt("To begin game, select Rock, Paper, or Scissors");
return ask.toLowerCase();
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "scissors") {
return "You win!";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You lose";
} else if (playerSelection == "scissors" && computerSelection == "paper") {
return "You win!";
} else if (playerSelection == "scissors" && computerSelection == "rock") {
return "You lose";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You win!";
} else if (playerSelection == "paper" && computerSelection == "scissors") {
return "You lose";
} else if (playerSelection == computerSelection) {
return "It's a tie";
} else {
return "Try again";
}
}
const playerSelection = userInput();
const computerSelection = getComputerChoice();
console.log(computerSelection);
console.log(playRound(playerSelection, computerSelection));

Conditional console.log message always displays itself

I am trying to create a simple rock, paper, scissors game to be played in the console. I am attempting to create an "invalid" message when a player inputs anything other than "rock," "paper," or "scissors."
Here is the if statement that I have devised:
if (playerSelection !== "rock", "paper", "scissors") {
console.log("Invalid")
This code displays the console.log message in all cases. I can't figure out what the problem is here.
Here is the whole code:
//create variable for each object
const getComputerChoice = ["rock", "paper", "scissors"]
//select one variable randomly
const computerSelection = Math.floor(Math.random() *
getComputerChoice.length);
//create prompt
const playerSelection = prompt("Rock, Paper, or
Scissors?").toLowerCase();
//player selection is rock
if (playerSelection === "rock" && getComputerChoice[computerSelection]
=== "rock") {
console.log("It's a tie!")
} else if (playerSelection === "rock" &&
getComputerChoice[computerSelection] === "paper") {
console.log("You lose! Paper beats rock")
} else if (playerSelection === "rock" &&
getComputerChoice[computerSelection] === "scissors") {
console.log("You win! Rock beats scissors")
}
//player selection is paper
if (playerSelection === "paper" && getComputerChoice[computerSelection]
=== "rock") {
console.log("You win! Paper beats rock")
} else if (playerSelection === "paper" &&
getComputerChoice[computerSelection] === "paper") {
console.log("It's a tie!")
} else if (playerSelection === "paper" &&
getComputerChoice[computerSelection] === "scissors") {
console.log("You lose! Scissors beats paper")
}
//player selection is scissors
if (playerSelection === "scissors" &&
getComputerChoice[computerSelection] === "rock") {
console.log("You lose! Rock beats scissors")
} else if (playerSelection === "scissors" &&
getComputerChoice[computerSelection] === "paper") {
console.log("You win! Scissors beats paper")
} else if (playerSelection === "scissors" &&
getComputerChoice[computerSelection] === "scissors") {
console.log("It's a tie!")
}
//player inputs invalid term
if (playerSelection !== "rock", "paper", "scissors") {
console.log("Invalid")
}
Your if condition is incorrect.
There are multiple solutions:
Use includes():
let playerSelection = "rock";
if (!["rock", "paper", "scissors"].includes(playerSelection)) {
console.log("Invalid")
} else {
console.log("Valid")
}
Use NOT (!) and logical OR (||).
let playerSelection = "rock";
const isValidChoice = playerSelection === "rock" ||
playerSelection === "paper" ||
playerSelection === "scissors";
if (!isValidChoice) {
console.log("Invalid")
} else {
console.log("Valid")
}
Use a Set. Please note: This is less performant for the three items in the array here, but if you have many items to compare to this will be the fastest option as lookups are performed in O(1) instead of O(n) (as is the case for includes()).
let playerSelection = "rock";
// create set once, then only use for lookups
const possibleChoices = new Set(["rock", "paper", "scissors"]);
if (!possibleChoices.has(playerSelection)) {
console.log("Invalid")
} else {
console.log("Valid")
}
The "inversion" according to De Morgan's laws will of course also work.

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());
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));

I can't assign a function of random numbers to appear when I enter the variable I set it as

I have created the below code so that the computer plays 3 different random outcomes
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}
So far so good!, then I want to run another function where there will be player VS computer
function playRound() {
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} etc. etc.
I have declared these two variables for player and computer so that the above function works.
let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();
When I console.log(computerSelection), I always get the same result and no random values, but when I console log computerPlay() it works just fine and I am getting random outcomes. Why is this happening to me? :(
Below is the whole code I have written so far
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else(computerRandomNumber === 2)
return "scissors";
}
let playerSelection = ["rock", "paper", "scissors"];
let computerSelection = computerPlay();
function playRound() {
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else(playerSelection === "scissors" && computerSelection === "scissors")
return "It's a tie! Ties are lame and so are you for tying. "
}
You assign a variable once and never change it.
if you want the computer to make a decision on each round, you could write something like this:
function computerPlay() {
let computerRandomNumber = Math.floor(Math.random() * 3)
if (computerRandomNumber === 0) {
return "rock";
} else if (computerRandomNumber === 1) {
return "paper";
} else {
return "scissors";
}
}
let playerSelectionVariants = ["rock", "paper", "scissors"];
function playRound() {
let computerSelection = computerPlay();
let playerSelection = /* request user input from playerSelectionVariants */
if (playerSelection === "rock" && computerSelection === "rock") {
return "It's a tie! Ties are lame and so are you for tying."
} else if (playerSelection === "rock" && computerSelection === "paper") {
return "Loooooser looooser nia nia nia! Paper wrapped your rock and threw it in the bin!"
} else if (playerSelection === "rock" && computerSelection === "scissors") {
return "Bravoooo you made it! Rock smashed the scissors! "
} else if (playerSelection === "paper" && computerSelection === "rock") {
return "Bravoooo you made it! Paper wrapped the rock and threw it in the bin! "
} else if (playerSelection === "paper" && computerSelection === "paper") {
return "It's a tie! Ties are lame and so are you for tying. "
} else if (playerSelection === "paper" && computerSelection === "scissors") {
return "Loooooser looooser nia nia nia! Scissors cut through your paper and made it look like an ugly origami swan"
} else if (playerSelection === "scissors" && computerSelection === "rock") {
return "Loooooser looooser nia nia nia! Rock smashed the scissors. "
} else if (playerSelection === "scissors" && computerSelection === "paper") {
return "Bravoooo you made it! Scissors cut through paper and made it look like an ugly origami swan"
} else {
return "It's a tie! Ties are lame and so are you for tying. "
}
}

How to display in console the result of ROCK, PAPER, SCISSOR GAME Javascript

I`m currently stuck with a rock, paper, scissors game in javascript.
I need to display in console something like this:
Computer choice: “Rock”
User choice: “Paper”
User wins!
How can I implement this in the code below.
let userSelection = userPlay();
function userPlay() {
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
let computerSelection = computerPlay();
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "Computer wins!";
} else {
return "User wins!";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "User wins!";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "Computer wins!";
}
} else {
if (computerSelection === "rock") {
return "Computer wins!";
} else if (computerSelection === "paper") {
return "User wins!";
} else {
return "Draw!";
}
}
}
console.log(playRound(userPlay, computerSelection));
I have swapped the position of function calls for better readability. Idea is to store the result of each function call into a variable and print it.
function userPlay() {
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function computerPlay() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
if (playerSelection === "rock") {
if (computerSelection === "rock") {
return "Draw!";
} else if (computerSelection === "paper") {
return "Computer wins!";
} else {
return "User wins!";
}
} else if (playerSelection === "paper") {
if (computerSelection === "rock") {
return "User wins!";
} else if (computerSelection === "paper") {
return "Draw!";
} else {
return "Computer wins!";
}
} else {
if (computerSelection === "rock") {
return "Computer wins!";
} else if (computerSelection === "paper") {
return "User wins!";
} else {
return "Draw!";
}
}
}
var userChoice = userPlay();
var computerSelection = computerPlay();
var result = playRound(userChoice, computerSelection)
console.log("user's choice", userChoice);
console.log("computer's choice", computerSelection);
console.log("Result is", result);
I'm not sure if I'm too late for an answer but I have refactored the code a bit (it can be refactored a bit more I think, but I'll let you do that).
let playerSelection = getNumber();
let computerSelection = getNumber();
function getNumber() { //computer generates a random answer.
let random = ["rock", "paper", "scissors"];
return random[Math.floor(Math.random() * 3)];
}
function playRound(playerSelection, computerSelection) { //plays a round of the game.
let message = '';
let random = ["rock", "paper", "scissors"];
let playerSelectionIndex = random.indexOf(playerSelection);
let computerSelectionIndex = random.indexOf(computerSelection);
switch (true) {
case(playerSelectionIndex == computerSelectionIndex + 1):
// Scissors beats rock
case((playerSelectionIndex + 2) == computerSelectionIndex):
message = 'You Win';
break;
case (computerSelectionIndex == playerSelectionIndex):
message = 'Draw';
break;
default:
message = 'You lose';
break;
}
return message;
}
console.log(playRound(playerSelection, computerSelection));

Categories