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.
Related
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));
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.
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.
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