Rock paper scissor cannot read properties of undefined (reading 'includes')

In my rock paper scissor game the user has to enter rock, paper or scissor 5 times then a message shown who won.Sometimes I can write rock, paper, scissor 5 times in a row and it works fine but sometimes I have this message:

here is my js code:


function getComputerChoice() {
    let rockRandomNumber = Math.floor(Math.random() * 10000);
    let paperRandomNumber = Math.floor(Math.random() * 10000);
    let scissorRandomNumber = Math.floor(Math.random() * 10000);
    let randomChoice;
    if (rockRandomNumber > paperRandomNumber && rockRandomNumber > scissorRandomNumber) {
        randomChoice = "Rock";
    } else if (paperRandomNumber > rockRandomNumber && rockRandomNumber > scissorRandomNumber) {
        randomChoice = "Paper";
    } else {
        randomChoice = "Scissor";
    }
    return randomChoice;
}

function playRound(playerSelection, computerSelection) {
    playerSelection = playerSelection.toLowerCase();
    computerSelection = computerSelection.toLowerCase();
    let counter = 0;
    let result;
    while (counter != 1) {
        if (playerSelection === "rock" && computerSelection === "scissor") {
            result = "You won! Rock beat Scissor";
            counter++;
        } else if (playerSelection === "paper" && computerSelection === "rock") {
            result = "You won! Paper beat Rock";
            counter++
        } else if (playerSelection === "scissor" && computerSelection === "paper") {
            result = "You won! Scissor beat Paper";
            counter++
        } else if (playerSelection === "paper" && computerSelection === "scissor") {
            result = "You lost! Scissor beat Paper";
            counter++
        } else if (playerSelection === "rock" && computerSelection === "paper") {
            result = "You lost! Paper beat Rock";
            counter++
        } else if (playerSelection === "scissor" && computerSelection === "rock") {
            result = "You lost! Rock beat Scissor";
            counter++
        } else {
            computerSelection = getComputerChoice();
            computerSelection = computerSelection.toLowerCase();
            break;
        }
    }
    return result;
}

function finalResult(playerCounter, computerCounter) {
    if (playerCounter > computerCounter) {
        return "Congratulation, you beat the computer";
    } else {
        return "You lost, better luck next time";
    }
}

function game() {
    let playerCounter = 0;
    let computerCounter = 0;
    let userPrompt = prompt("Enter rock, paper or scissor");
    let computerSelection = getComputerChoice();
    let result;
    let containtWonOrNot;
    let won = "won"
    for(let i = 0; i < 5; i++) {
        result = playRound(userPrompt, computerSelection);
             containtWonOrNot = result.includes(won);
        if (containtWonOrNot === true) {
            console.log(result);
            playerCounter++;
        } else {
            console.log(result)
            computerCounter++;
        }
        userPrompt = prompt("Enter rock, paper or scissor");
        computerSelection = getComputerChoice();   
        console.log(playerCounter + "-" + computerCounter);
    } 
    console.log(finalResult(playerCounter, computerCounter));
}

game();

and html code:

`<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Rock paper scissors</title>
</head>
<body>
    <script src="js/script.js"></script>
</body>
</html>`

When I write in the user prompt I write like that: rock, paper, scissor

If it is a tie the else runs which does not produce a value for result and the function returns undefined which you then call includes on inside game.

If you have accounted for all the won/lost combinations in the if statements the else can handle the tie (if nobody won/lost it must be a tie) so set the result to that and handle that case in the game as well.

1 Like

Thanks for the answer it worked

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.