Rock, Paper and Scissors!

Hey everyone,
It’s been awhile since I practice Javascript and just now I’m writing rock and paper scissors code for exercising.

it’s almost perfect, only one tiny issues. before that, i would like to describe the game im making:

  1. The rock, paper and scissors is put in 5 round
  2. each round it will shows :
    • computer choice of rock paper and scissors
    • Whether you win or lose
    • the score of player and computer
  3. By the end of the 5 round of game, it will conclude if you’re winning or losing in the whole set of the game.

Problem:
the conclusion isn’t working, it will always conclude that I’m winning (if im putting else if in the game() function) or It’s a tie (if it’s kept if in the game() function). looks like the score number is just looping through the round instead of increasing the score based on the game result.

I need to fix the score number loop. Could anyone please let me know where i’m doing wrong?

Here’s my code:

function playRound(playerSelection,computerSelection) {
    if (playerSelection.toUpperCase() === computerSelection.toUpperCase()) {
        return "it's a draw!";
    } else if ((playerSelection.toLowerCase() === 'rock' && computerSelection === 'scissors')) {
        return "You win! Rock beats scissors.";
    } else if ((playerSelection.toLowerCase() === 'rock' && computerSelection === 'paper')) {
        return "You lose! Paper beats rock.";
    } else if ((playerSelection.toLowerCase() === 'paper' && computerSelection === 'rock')) {
        return "You win! Paper beats rock.";
    } else if ((playerSelection.toLowerCase() === 'paper' && computerSelection === 'scissors')) {
        return "You lose! Scissors beats paper.";
    } else if ((playerSelection.toLowerCase() === 'scissors' && computerSelection === 'paper')) {
        return "You win! Scissors beats paper.";
    } else if ((playerSelection.toLowerCase() === 'scissors' && computerSelection === 'rock')) {
        return "You lose! Rock beats scissors.";
    }
}

function game() {
    let scorePlayer = 0;
    let scoreComputer = 0;
    for (let i =0; i<5 ; i++){
        const playerSelection = playerChoice();
        const computerSelection = getComputerChoice();
        console.log(computerSelection);
        console.log(playRound(playerSelection,computerSelection));
        console.log(`Player ${scorePlayer} VS Computer ${scoreComputer}`);
        if((playRound(playerSelection,computerSelection) === "You win! Rock beats scissors."||"You win! Paper beats rock."||"You win! Scissors beats paper.")){
            scorePlayer++;
        }
        else if((playRound(playerSelection,computerSelection) === "You lose! Rock beats scissors."||"You lose! Paper beats rock."||"You lose! Scissors beats paper.")){
            scoreComputer++;
        }
    }
    console.log("Game Over")
    if(scorePlayer > scoreComputer){
        console.log("Congrats you win!!");
    } else if (scorePlayer < scoreComputer){
        console.log("Better luck next time.");
    } else {
        console.log("it's a tie!");
    }
}

I’m not showing the playerchoice() and getComputerChoice() code as they’re doing fine. but feel free to let me know if that’s needed.

Thank you in Advance!

Showing only part of the code is often making things harder or impossible for somebody who might want to help.

Having that said, your analysis seem to be accurate - during each loop iteration the score of player is incremented. Take a closer look at the part that’s responsible for it, try to figure out why that’s happening. Some help might give you adding printing to console the result of the conditions check.

1 Like

Hey there,
I personally think that the problem lies here,

if((playRound(playerSelection,computerSelection) === "You win! Rock beats scissors."||"You win! Paper beats rock."||"You win! Scissors beats paper.")){
            scorePlayer++;
        }
        else if((playRound(playerSelection,computerSelection) === "You lose! Rock beats scissors."||"You lose! Paper beats rock."||"You lose! Scissors beats paper.")){
            scoreComputer++;
        }

But i’m not sure how or why. my logic is that, if the result of Playround is “You win! Rock beats scissors.” or “You win! Paper beats rock.” or “You win! Scissors beats paper.” means the player won and therefore the Player score should increase by 1.

while if the result is it’s “You lose! Rock beats scissors.” or “You lose! Paper beats rock.” or “You lose! Scissors beats paper.”, it should be computer score gets increased.

but it seems that this code didn’t runs well, Hence, the loop completely ignores it.
as for playerchoice() and getComputerChoice() code, Here is it for the convenience to try the code out:

const options = ["rock","paper","scissors"];

function getComputerChoice(){
    const random = options[Math.floor(Math.random()*options.length)];
    return random;
}

function playerChoice(){
    let validatedInput = false;
    while(validatedInput == false){
        const choice = prompt("Rock, Paper or Scissors?");
        if (choice == null){
            continue;
        }
        const choiceInLower = choice.toLowerCase();
        if (options.includes(choiceInLower)){
            validatedInput = true;
            return choiceInLower;
        }
    }
}

The way you are describing the logic seems correct. Not necessarily the code is doing just that. Try printing to console the condition from if, take a look at it for a couple different rounds.

console.log(playRound(playerSelection,computerSelection) === "You win! Rock beats scissors."||"You win! Paper beats rock."||"You win! Scissors beats paper.")

Hi there, thank you for the hint.
when i print

console.log(playRound(playerSelection,computerSelection) === "You win! Rock beats scissors."||"You win! Paper beats rock."||"You win! Scissors beats paper.")

It shows that if the rock actually beat something, it would return true value, but aside from that it will always return You win! Paper beats rock. no matter what.

I tried to put "You win! Rock beats scissors."||"You win! Paper beats rock."||"You win! Scissors beats paper." into one bracket and it didn’t work either.

and at last what i did was separate it one by one like the code below to make it work:

if((playRound(playerSelection,computerSelection) === ("You win! Rock beats scissors."))||(playRound(playerSelection,computerSelection) === ("You win! Paper beats rock."))||(playRound(playerSelection,computerSelection) === ("You win! Scissors beats paper."))){
            scorePlayer++;
        } 

Here’s the complete game() code for the convenient of trying it out:

function game() {
    let scorePlayer = 0;
    let scoreComputer = 0;
    for (let i =0; i<5 ; i++){
        const playerSelection = playerChoice();
        const computerSelection = getComputerChoice();
        console.log(computerSelection);
        console.log(playRound(playerSelection,computerSelection));
        console.log(`Player ${scorePlayer} VS Computer ${scoreComputer}`);
        if((playRound(playerSelection,computerSelection) === ("You win! Rock beats scissors."))||(playRound(playerSelection,computerSelection) === ("You win! Paper beats rock."))||(playRound(playerSelection,computerSelection) === ("You win! Scissors beats paper."))){
            scorePlayer++;
        } 
        else if((playRound(playerSelection,computerSelection) === ("You lose! Rock beats scissors."))||(playRound(playerSelection,computerSelection) === ("You lose! Paper beats rock."))||(playRound(playerSelection,computerSelection) === ("You lose! Scissors beats paper."))){
            scoreComputer++;

        }
    }
    console.log("Game Over")
    if(scorePlayer > scoreComputer){
        console.log("Congrats you win!!");
    } else if (scorePlayer < scoreComputer){
        console.log("Better luck next time.");
    } else {
        console.log("it's a tie!");
    }
}

I’m glad it finally works, but could you help me to explain how without brackets and different brackets placement resulting in different result? because i don’t quite understand the reason behind it.

The this is the

(
    playRound(playerSelection,computerSelection) === "You win! Rock beats scissors."
    || "You win! Paper beats rock."
    || "You win! Scissors beats paper."
)

Is evaluated in this order:

  1. Is playRound(playerSelection,computerSelection) === "You win! Rock beats scissors." truthy?
  2. OR, evaluate: is "You win! Paper beats rock." truthy? (Any non-empty string is always truthy.)
  3. OR, if somehow it wouldn’t be, finally evaluate: is "You win! Scissors beats paper." truthy? (It also always would be truthy)

It is not:

  1. playRound(...) === "You win! Rock beats scissors."
  2. OR playRound(...) === "You win! Paper beats rock."
  3. OR playRound(...) === "You win! Scissors beats paper.".
1 Like

aha! i think i understand it a little more better now. thank you so much for sticking with me with this one! ^^

1 Like