I am trying to compare a clicked button's ID to a random string output from another function and log the result in the console (it's a rock, paper, scissors game); Code depicted below
buttons.forEach((button) => {
button.addEventListener("click", () => {
console.log(playRound(event.srcElement.id));
});
});
function playRound(playerSelection) {
const computerSelection = computerPlay;
if (playerSelection === 'rock' && computerSelection === 'rock') {
return 'Tie Game!';
} else if (playerSelection === 'rock' && computerSelection === 'paper') {
return 'Paper beats rock! You lose!';
} else if (playerSelection === 'rock' && computerSelection === 'scissors') {
return 'Rock beats scissors! You win!';
} else if (playerSelection === 'paper' && computerSelection === 'rock') {
return 'Paper beats rock! You win!';
} else if (playerSelection === 'paper' && computerSelection === 'paper') {
return 'Tie Game!';
} else if (playerSelection === 'paper' && computerSelection === 'scissors') {
return 'Scissors beats paper! You lose!';
} else if (playerSelection === 'scissors' && computerSelection === 'rock') {
return 'Rock beats Scissors! You lose!';
} else if (playerSelection === 'scissors' && computerSelection === 'paper') {
return 'Scissors beats paper! You win!';
} else if (playerSelection === 'scissors' && computerSelection === 'scissors') {
return 'Tie Game!';
}
}
function computerPlay() {
const play = (Math.floor(Math.random() * 3));
if (play === 0) {
return 'rock';
}
else if (play === 1) {
return 'paper';
}
else if (play === 2) {
return 'scissors';
}
}
I know this code is a bit inefficient but my problem is that the playRound() function returns undefined when I execute this. I've tried logging the button's ID alone, which worked, I've tried passing the button's ID as a parameter to a dummy function, which worked, and I tried logging the computerPlay() output to make sure it was returning a string (it was). I can't tell why my playRound() function is unable to compare the two strings given and return a result string.
Related
I am trying to make a small rock, paper and scissors program between a user and the computer. Everything seems to work fine except the gameRound() conditional statement. It doesn't matter what the user input is. It just runs the (else) statement.
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
// the logic that decides who wins and loses
const gameRound = (playerSelection, computerSelection) => {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// the function that begins the game
const game = () => {
for(let i = 0; i < 5; i++) {
return gameRound();
}
}
console.log(game());
gameRound conditional statement doesn’t function as expected due to the newly declared parameters playerSelection, computerSelection. So it has to be a function statement rather than the inline arrow function.
Then, Javascript code would be as follows:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
for (let i = 0; i < 5; i++) {
console.log(gameRound(playerSelection, computerSelection));
}
Or if you mean to play 5 rounds of the rock-paper-scissors, the allocation of playerSelection and computerSelection variables should be inside the for loop as follows:
// a function that determines what the computer's choice is
const getComputerChoice = () => {
let compChoice = Math.random();
if(compChoice > 0.5) {
return "Rock";
} else if(compChoice < 0.5) {
return "scissors";
} else if(compChoice == 0) {
return "paper";
}
}
// the logic that decides who wins and loses
function gameRound(playerSelection, computerSelection) {
if(playerSelection === "rock" && computerSelection === "rock" || playerSelection === "paper" && computerSelection === "paper" || playerSelection === "scissors" && computerSelection === "scissors") {
return "It's a tie!!!";
} else if(playerSelection === "rock" && computerSelection === "paper") {
return "Computer wins!!!";
} else if(playerSelection === "rock" && computerSelection === "scissors") {
return "You win!!!";
} else if(playerSelection === "paper" && computerSelection === "scissors") {
return "Computer wins!!!";
} else if(playerSelection === "paper" && computerSelection === "rock") {
return "You win!!!";
} else if(playerSelection === "scissors" && computerSelection === "rock") {
return "Computer wins!!!";
} else if(playerSelection === "scissors" && computerSelection === "paper") {
return "You win!!!";
} else {
return "Winner undecided";
}
}
for (let i = 0; i < 5; i++) {
// prompts the user to enter rock, paper or scissors
let playerSelection = prompt();
let computerSelection = getComputerChoice();
console.log(gameRound(playerSelection, computerSelection));
}
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));
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Greeting all, I'm doing Odin Project and had rage quitted couple times without finding actual solution for this one. And finally after couple days I dedicated myself to post here for answer.
Maybe someone could explain where is a problem and why I get always output "It is tie" ?
Can't figure it out where is the problem.
function compPlay() {
const number = (Math.random() * 3);
if (number <= 1) {
return 'Rock';
} else if (number >= 2) {
return 'Paper';
} else return 'Scissors';
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == 'rock' && computerSelection == 'Scissors') {
return 'You Won ! Rock beats Scissors.';
} else if (playerSelection == 'scissors' && computerSelection == 'Rock') {
return 'You Lose ! Rock beats Scissors.';
} else if (playerSelection == 'paper' && computerSelection == 'Rock') {
return 'You Won ! Paper beats Rock.';
} else if (playerSelection == 'rock' && computerSelection == 'Paper') {
return 'You Lost ! Paper beats Rock.';
} else if (playerSelection == 'scissors' && computerSelection == 'Paper') {
return 'You Won ! Scissors beats Paper.';
} else if (playerSelection == 'paper' && computerSelection == 'Scissors') {
return 'You Lost ! Scissors beats Paper.';
} else {
return 'It is tie';
}
}
const playerSelection = window.prompt("Rock, Paper or Scissors:");
const computerSelection = compPlay();
console.log(playRound(playerSelection, computerSelection));
You should update your compPlay function to round your number to whole numbers. Then you can do direct number comparisons and hit each case. You also need to trim any trailing whitespace and lower case your user input so your comparisons actually match up. I would also check explicitly for a tie by checking user input equals to computer input and dump anything else as no match.
function compPlay() {
const number = Math.ceil((Math.random() * 3));
if (number == 1) {
return 'Rock';
} else if (number == 2) {
return 'Paper';
} else return 'Scissors';
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == 'rock' && computerSelection == 'Scissors') {
return 'You Won ! Rock beats Scissors.';
} else if (playerSelection == 'scissors' && computerSelection == 'Rock') {
return 'You Lose ! Rock beats Scissors.';
} else if (playerSelection == 'paper' && computerSelection == 'Rock') {
return 'You Won ! Paper beats Rock.';
} else if (playerSelection == 'rock' && computerSelection == 'Paper') {
return 'You Lost ! Paper beats Rock.';
} else if (playerSelection == 'scissors' && computerSelection == 'Paper') {
return 'You Won ! Scissors beats Paper.';
} else if (playerSelection == 'paper' && computerSelection == 'Scissors') {
return 'You Lost ! Scissors beats Paper.';
} else if (playerSelection == computerSelection.toLowerCase()) {
return 'It is tie';
} else {
return 'No Match'
}
}
const playerSelection = (window.prompt("Rock, Paper or Scissors:")).trim().toLowerCase();
const computerSelection = compPlay();
console.log(playRound(playerSelection, computerSelection));
In the final else, print out playerSelection and computerSelection in quotes. It's most likely a case issue, but you can easily find out what you're comparing.
I'd also suggest modifying the logic a little to make it easier on yourself:
if(playerSelection === 'rock') {
if(computerSelection === 'rock') {
console.log('tie!');
} else if(computerSelection === 'paper') {
console.log('you lose!')
} else if(computerSelection === 'scissors') {
console.log('you win!');
}
} else if(playerSelection === 'paper') {
...
} else {
...
}
It's a little more verbose, but much easier to read and figure out what's going on.
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. "
}
}
Hey StackOverflow community!
I'm new here and just started learning JS via Odin Project. I'm on my first JS project (rock, paper, scissors app) and am stuck....
I'll summarize what my intentions are below followed by my code.
//The user would input a value of either rock, paper, or scissors
//With the user value, it would be compared to the value of the computer's selection at random
//Depending on that comparison from both the user's input and the computer generated one, a return statement would be given
Have tried almost everything and not sure how to move forward! Please help.
function computerPlay() {
let gameOptions = ['rock', 'paper', 'scissors'];
const gameChoice = Math.floor(Math.random() * gameOptions.length);
console.log(gameChoice, gameOptions[gameChoice]);
}
function playRound(playerSelection,computerSelection){
if(playerSelection === 'rock' && computerSelection === 'paper') {
return console.log('You lose! Paper beats rock.');
}else if(playerSelection === 'paper' && computerSelection === 'scissors') {
return console.log('You lose! Scissors beats paper.');
}else if(playerSelection === 'scissors' && computerSelection === 'rock') {
return console.log('You lose! Rock beats scissors');
}else if(playerSelection === 'rock' && computerSelection === 'rock') {
return console.log('Its a draw!');
}else if(playerSelection === 'paper' && computerSelection === 'paper') {
return console.log('Its a draw!');
}else if(playerSelection === 'scissors' && computerSelection === 'scissors') {
return console.log('Its a draw!');
}else if(playerSelection === 'paper' && computerSelection === 'rock') {
return console.log('You win!')
}else if(playerSelection === 'rock' && computerSelection === 'scissors') {
return console.log('You win!')
}else {
return console.log('You win!')
}
}
const playerSelection = prompt('Choose either rock, paper, or scissors', '');
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
I was hoping the conditional statement within the 2nd function would be able to accomplish this. Still trying to wrap my head around functions, parameters, and arguments.
Any and all help would be GREATLY appreciated!! Thanks.
computerPlay() does not return any value. So computerSelection will be undefined.
Also, change this line console.log(playRound(playerSelection, computerSelection)); to just playRound(playerSelection, computerSelection); and remove the return statements from playRound().
Also, your logic can be greatly simplified. For example, if (playerSelection === computerSelection) console.log("It's a draw");
computerPlay function does not return any value. I think you should console each value before do playRound