Rock, Paper, Scissors game in Javascript - javascript

I am making a Rock paper, and scissors game for the Odin project which is supposed to run in the console.
Currently, everything works fine until I implemented the function called game().
I assumed it would call the playRound() function until playerscore, or computerscore reached 5. but it is currently only working twice. It prompts once up top for playerSelection and prompts again for playerSelection within function game();
How could I get game() to run until either playerscore or computerscore reaches 5? And display result for each round?
let playerScore = 0;
let computerScore = 0;
function computerPlay() {
let choices = ['rock', 'paper', 'scissors']
return choices[Math.floor(Math.random() * choices.length)]
}
let computerSelection = computerPlay();
let playerSelection = prompt("Make your selection, Rock, paper or Scissors").toLowerCase();
function playRound(playerSelection, computerSelection){
if ((playerSelection == "rock" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "rock")){
playerScore++;
return ("You win! " + playerSelection + " beats " + computerSelection + ", the score is Player:" + playerScore + " Computer:" + computerScore);
} else if (playerSelection == computerSelection){
return "Draw!"
} else {
computerScore++
return ("You lose! " + computerSelection+ ", beats " + playerSelection + " the score is Player:" + playerScore + " Computer:" + computerScore)
}
}
function game(){
if (playerScore < 5 && computerScore < 5) {
let playerSelection = prompt("Make your selection, Rock, paper or Scissors").toLowerCase();
playRound(playerSelection, computerSelection)
}
}
game();
console.log(playRound(playerSelection,computerSelection));

Related

My code is not running but i have no errors

The project is about making a rock paper scissors game using functions and loops etc.
the code below is how the project looks
the first function is to get the randomized computer choice
the second function is to get the user or players choice
the third function is to play the game and check if the player won or the computer won
the final function is to create a for loop to run the third function a certain number of times to see who is the winner of the game
Been working on it since and have no idea why it doesn't work
```
function getComputerChoice() {
let values = ["rock", "paper", "scissors"];
return values[Math.floor(Math.random() * values.length)];
}
function getPlayerChoice() {
let getChoice = "rock";
let value = getChoice.trim();
let lowCase = value.toLowerCase();
let capitalize = lowCase.charAt(0).toUpperCase + lowCase.slice(1);
while (!["Rock", "Paper", "Scissors"].includes(capitalize)) {
value = getChoice.trim();
lowCase = value.toLowerCase();
capitalize = lowCase.charAt(0).toUpperCase + lowCase.slice(1);
}
return capitalize;
}
function playRound(playerSelection, computerSelection) {
let games = "";
if (
(playerSelection == "rock" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "rock")
) {
return (games =
"player loses!! " + computerSelection + " beats " + playerSelection);
} else if (
(playerSelection == "paper" && computerSelection == "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")
) {
return (games =
"player Wins!! " + playerSelection + " beats " + computerSelection);
} else if (playerSelection == computerSelection) {
return (games =
"it a tie noice " + playerSelection + " v " + computerSelection);
} else {
return (games = "euphoria");
}
}
function game() {
let playerScores = 0;
let computerScores = 0;
let computerSelection = "";
let playerSelection = "";
computerSelection = getComputerChoice();
playerSelection = getPlayerChoice();
for (let i = 0; i < 5; i++) {
if (
playRound(
"player loses!! " + computerSelection + " beats " + playerSelection
)
) {
computerScores += 1;
console.log(
"you lost this round!! boo hoo!! scores are " +
computerScores +
" v " +
playerScores
);
} else if (
playRound(
"player Wins!! " + playerSelection + " beats " + computerSelection
)
) {
playerScores += 1;
console.log(
"you Won this round!! hurray!! scores are " +
computerScores +
" v " +
playerScores
);
}
}
if (playerScores > computerScores) {
console.log("congratulations you won this round");
} else if (playerScores < computerScores) {
console.log("im sorry you lost this round");
} else {
console.log("there is a problem");
}
}
game();
```
It doesn't seem that playRound() is returning anything:
function playRound(playerSelection, computerSelection) {
let games = '';
if (
(playerSelection == "rock" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "rock")
) {
games = "player loses!! " + computerSelection + " beats " + playerSelection;
// ADD RETURN HERE
} else if (
(playerSelection == "paper" && computerSelection == "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")
) {
games = "player Wins!! " + playerSelection + " beats " + computerSelection;
// ADD RETURN HERE
} else if (playerSelection == computerSelection) {
games = "it a tie noice " + playerSelection + " v " + computerSelection;
// ADD RETURN HERE
} else {
games = "euphoria";
// ADD RETURN HERE
}
}
Explanation
In your game() function definition you are checking if(playRound()). playRound is not returning anything so this is interpreted as a void function, hence, the if will always evaluate to false.

Can't seem to figure out how to keep score in Rock, Paper, Scissors

I am trying to make Rock, Paper, Scissors via The Odin Project, and I'm stuck in the last step. I don't seem to have trouble getting the prompt to work, just can't seem to either tally up points, or I'm having trouble ending the game when either the player or computer gets to 5 points. I did include some console.logs at the bottom, but I'm unable to actually see the score due to the endless prompts. What am I doing wrong? I just need the game to end when either the player or computer gets to 5 points.
let playerScore = 0;
let computerScore = 0;
function computerPlay() {
let allChoices = ["Rock", "Paper", "Scissors"];
let randomChoice = allChoices[Math.floor(Math.random() * allChoices.length)];
return randomChoice
}
function game() {
while (playerScore <= 5 || computerScore <= 5) {
const playerSelection = prompt("Would you like to choose R, P or S?")
const computerSelection = computerPlay();
alert(playRound(playerSelection, computerSelection));
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection == computerSelection) {
return "Tie game!"
} else if (playerSelection == "Rock" && computerSelection == "Scissors") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else if (playerSelection == "Paper" && computerSelection == "Rock") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else if (playerSelection == "Scissors" && computerSelection == "Paper") {
return `You win! ${playerSelection} beats ${computerSelection}!`
playerScore += 1;
} else {
return `You lose! ${computerSelection} beats ${playerSelection}`
computerScore += 1;
}
}
function winGame() {
if (playerScore == 5) {
return "You win!"
} else if (computerScore == 5) {
return "You lose!"
}
}
game();
console.log(playerScore);
console.log(computerScore);
function game() {
while (playerScore < 5 && computerScore < 5) {
const playerSelection = prompt("Would you like to choose Rock, Paper or Scissors?")
const computerSelection = computerPlay();
alert(playRound(playerSelection, computerSelection));
}
}
function playRound(playerSelection, computerSelection) {
if (playerSelection === computerSelection) {
return "Tie game!"
} else if (playerSelection === "Rock" && computerSelection === "Scissors") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else if (playerSelection === "Paper" && computerSelection === "Rock") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else if (playerSelection === "Scissors" && computerSelection === "Paper") {
playerScore += 1;
return `You win! ${playerSelection} beats ${computerSelection}!`
} else {
computerScore += 1;
return `You lose! ${computerSelection} beats ${playerSelection}`
}
}
You returned from the function before incrementing the points. So they will never be incremented. Also (playerScore <= 5 || computerScore <= 5) means, that the game will go on until both - playerScore and computerScore are below 6. It has to stop when one of them reaches 5

Rock Paper Scissors game only one outcome unless I refresh page

So im making The Odin Project's rock paper scissors game and the only problem i have is that the computer's selection is always the same unless I refresh the page.
for example if i choose Rock three times in a row, the computer's selection will always be the same unless i refresh the page.
const thing1 = "Rock",
thing2 = "Paper",
thing3 = "Scissors";
function computerPlay() {
let a = Math.random();
if (a <= 0.33) {
return thing1;
} else if (a > 0.33 && a < 0.67) {
return thing2;
} else {
return thing3;
}
}
let playerScore = 0,
computerScore = 0;
const playerSelection = ["Rock", "Paper", "Scissors"];
let computerSelection = computerPlay();
function playRound(playerSelection, computerSelection) {
if (playerSelection.toLowerCase() == "rock" && computerSelection == thing2) {
computerScore++;
return "You lose! Rock loses to Paper";
} else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing3) {
playerScore++;
return "You win! Rock beats Scissors";
} else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing1) {
playerScore++;
return win = "You win! Paper beats Rock";
} else if (playerSelection.toLowerCase() == "paper" && computerSelection == thing3) {
computerScore++;
return win = "You lose! Paper loses to Scissors";
} else if (playerSelection.toLowerCase() == "rock" && computerSelection == thing1) {
computerScore++;
return win = "You lose! Rock loses Scissors";
} else if (playerSelection.toLowerCase() == "scissors" && computerSelection == thing2) {
playerScore++;
return win = "You win! Scissors beats Paper!";
} else {
return "Tie!";
}
}
function game() {
let playerSelection = prompt("Rock, paper or scissors?");
result = playRound(playerSelection, computerSelection);
scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
console.log(scoreboard);
return result;
}
You only call computerPlay() once, leading it computerSelection being the exact same every round. Call it every round to get a new random value.
function game() {
let playerSelection = prompt("Rock, paper or scissors?");
computerSelection = computerPlay(); // <---
result = playRound(playerSelection, computerSelection);
scoreboard = "User:" + " " + playerScore + " " + "Computer" + " " + computerScore;
console.log(scoreboard);
return result;
}

Rock, Paper, Scissors Game shows both win and lose messages

I'm currently working through a Rock, Paper, Scissors project that plays out in the console (this is for The Odin Project, front end coming soon).
Here is what my script looks like for reference:
<script>
function computerPlay() {
const choice = ["Rock", "Paper", "Scissors"]
return choice[Math.floor(Math.random() * choice.length)]
}
function play(playerSelection, computerSelection) {
const lose = console.log('You lose! ' + computerSelection + ' beats ' + playerSelection + '!')
const win = console.log('You win! ' + playerSelection + ' beats ' + computerSelection + '!')
if (playerSelection === computerSelection) {
return console.log("It's a draw! Try again!")
}
if (playerSelection === "rock" && computerSelection === "Paper") {
return lose
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
return win
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
return lose
}
if (playerSelection === "paper" && computerSelection === "Rock") {
return win
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
return lose
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
return win
}
}
function game() {
playerSelect = prompt("Welcome to Rock, Paper, Scissors! Which one do you choose? \n")
compSelect = computerPlay()
console.log("Player chose " + playerSelect)
console.log("Computer chose " + compSelect)
console.log(play(playerSelect, compSelect))
}
</script>
Right now my output is showing both the win and lose conditions for any given choice like this:
Player chose rock
Computer chose Rock
You lose! Rock beats rock!
You win! rock beats Rock!
I chose to keep the win and lose messages in variables but I know I probably screwed some small detail up there. I've tried adding/removing the if else statements but both messages still show up no matter what choice.
I'm planning on making either choice case insensitive until I solve this error.
Any help would be appreciated, thanks!:
When you are calling your console.log at the beginning of the script, you are not saving them for later, you are running them and assigning their return value to a variable. What you could do is have a win and a lose variable that are function and then call them when the player is winning/loosing.
function computerPlay() {
const choice = ["Rock", "Paper", "Scissors"];
return choice[Math.floor(Math.random() * choice.length)];
}
function play(playerSelection, computerSelection) {
// we are storing a function into the win and lose variable.
const lose = () => console.log('You lose! ' + computerSelection + ' beats ' + playerSelection + '!');
const win = () => console.log('You win! ' + playerSelection + ' beats ' + computerSelection + '!');
if (playerSelection === computerSelection) {
return console.log("It's a draw! Try again!")
}
if (playerSelection === "rock" && computerSelection === "Paper") {
return lose()
}
if (playerSelection === "rock" && computerSelection === "Scissors") {
return win()
}
if (playerSelection === "paper" && computerSelection === "Scissors") {
return lose()
}
if (playerSelection === "paper" && computerSelection === "Rock") {
return win()
}
if (playerSelection === "scissors" && computerSelection === "Rock") {
return lose()
}
if (playerSelection === "scissors" && computerSelection === "Paper") {
return win()
}
}
function game() {
playerSelect = 'rock';
compSelect = computerPlay();
console.log("Player chose " + playerSelect);
console.log("Computer chose " + compSelect);
// You don't need to console.log the return value of a console.log
play(playerSelect, compSelect);
}
game();
Try changing your lose and win variables to their string values instead of console.log("/message/").
Your lose and win variable declarations are already printing their messages instead of storing them, since your code calls an instance of console.log().
That way, when you return either win or lose, your console.log(play(playerSelect, compSelect)) will receive a string, and then print it.

Why is my Rock Paper Scissors game not working? It keeps returning 'You chose Rock, you win', how do i fix this?

I have been stuck on this for so long, basically its a human vs computer, I type in say Rock and it gives me you chose rock you win, even when i chose the others it still returns that, can someone help me to figure out why it keeps doing this?
let person = prompt ("Rock, Paper, Scissors");
// Computer makes a choice
function computerPlay () {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() *
compchoice.length)];
}
//Player vs Computer
function playRound (playerSelection, computerSelection) {
if (playerSelection === 'Rock' || computerSelection ===
'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' || computerSelection ===
'Rock')
{
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' || computerSelection ===
'Paper')
{
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (computerSelection === 'Rock' || playerSelection ===
'Scissors')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Paper' || playerSelection ===
'Rock')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === 'Scissors' || playerSelection ===
'Paper')
{
return 'Computer chose ' + computerSelection + ',' + 'Computer
wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
}else {
return 'Please chose Rock, Paper, or Scissors';
}
}
const playerSelection = 'rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
It should just play a regular game of rock paper scissors, if i chose rock and the computer chose paper the computer should win. For now i am trying to make it play just one round.
You're using or statements. You need to use and statements. Take for example your first line:
if (playerSelection === 'Rock' || computerSelection === 'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
}
It's saying:
if playerSelection equals Rock OR computerSelection equals Scissors return
So if playerSelection is Rock it'll return right there. What you need to use is an AND statement instead. Try this:
let playerSelection = prompt("Rock, Paper, Scissors");
// Computer makes a choice
function computerPlay() {
let compchoice = ['Rock', 'Paper', 'Scissors'];
return compchoice[Math.floor(Math.random() * compchoice.length)];
}
//Player vs Computer
function playRound(playerSelection, computerSelection) {
if (playerSelection === 'Rock' && computerSelection === 'Scissors') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Paper' && computerSelection === 'Rock') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (playerSelection === 'Scissors' && computerSelection === 'Paper') {
return 'You chose ' + playerSelection + ',' + ' You win!';
} else if (computerSelection === 'Rock' && playerSelection === 'Scissors') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === 'Paper' && playerSelection === 'Rock') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === 'Scissors' && playerSelection === 'Paper') {
return 'Computer chose ' + computerSelection + ',' + 'Computer wins!';
} else if (computerSelection === playerSelection) {
return 'Its a draw!';
} else {
return 'Please chose Rock, Paper, or Scissors';
}
}
//const playerSelection = 'Rock';
const computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));
Also there was a typo in your player selection on the third last line. The playerSelection string should be capitalized like the strings it's comparing too.

Categories

Resources