user20759768
user20759768

Reputation:

How do I fix game is not define error for in console game?

I'm currently trying to create an in console Rock, Paper, Scissors game in console but when I go live to make sure my code is working, I keep receiving an error message saying:

index.html:14 Uncaught ReferenceError: game is not defined
at HTMLButtonElement.onclick (index.html:14:32)

I have tried fixing my variables as at first I didn't defined the "choices" array in my code. To fix this I tried to declare the "choices" array and assign it the values 'rock','paper','scissors' before calling the "computerChoice" function.

Here's the updated js code:

//this will play the game
//play five rounds
//console based

let choices = ["rock", "paper", "scissors"];
let winners = [];

function game() {
for (let i = 1; i <= 5; i++) {
playRound(i);
}
document.querySelector("button").textContent = "Play New Game";
logWins();
}

function playRound(round) {
const playerSelection = playerChoice();
const computerSelection = computerChoice();
const winner = checkWinner(playerSelection, computerSelection);
winners.push(winner);
logRound(playerSelection, computerSelection, winner, round);
}

function playerChoice() {
//get input from the player
let input = prompt("Type Rock, Paper, or Scissors");
while (input == null) {
input = prompt("Type Rock, Paper or Scissors");
}
}

// this allows no input to show up if misspelled
// a word, or wrote anything in the input prompt

input = input.toLowerCase();
let check = validateInput(input);
console.log(input);
return input;

//get random input from the comp

function computerChoice() {
return choices[Math.floor(Math.random() * choices.length)];
}

function validateInput(choice) {
if (choices.includes(choice)) {
return true;
} else {
return false;
}
}

function checkWinner(choiceP, choiceC) {
if (choiceP === choiceC) {
return "Tie";
} else if (
(choiceP === "rock" && choiceC === "scissors") ||
(choiceP === "paper" && choiceC === "rock") ||
(choiceP === "scissors" && choiceC === "paper")
) {
return "Player";
} else {
return "Computer";
}
}

function logWins() {
let playerWins = winners.filter((item) => item == "Player").length;
let computerWins = winners.filter((item) => item == "Computer").length;
let ties = winners.filter((items) => item == "Ties").length;
console.log("Results:");
console.log("Player Wins", playerWins);
console.log("Computer Wins", computerWins);
console.log("Tie", ties);
}

function logRound(playerChoice, computerChoice, winner, round) {
console.log("Round:", round);
console.log("Player Chose:", playerChoice);
console.log("Computer Chose:", computerChoice);
console.log(winner, "Won the Round!");
console.log("--------------------------");
}

game();

I also noticed that the console says index.html, so I tried to research this and I have come up short with resolutions.

here's my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="stylesheet" href="styles.css" />
</head>

<body>
<div class="flex-container">
<h1>Rock Paper Scissors</h1>
<button onclick="game()">Play New Game</button>
</div>
<p>
Hi! This game is played within the console.
<br />
Instructions to play this game on <br />
Windows/Linux
<br />
press <strong>CTRL</strong> <em>+</em> <strong>SHIFT</strong> <em>+</em>
<strong>J</strong>
<br />
On Mac <br />
press <strong>Option</strong> <em>+</em> <strong>J</strong> <br />
This will open the console so you can start playing!
</p>

<script src="main.js"></script>
</body>
</html>

Not sure what the issues is, also was considering researching on how to create this game using Python, as it may deem more likely to work

Upvotes: 0

Views: 435

Answers (1)

msx47
msx47

Reputation: 36

You are returning outside a function that causes an error before the "game" function if defined.

function playerChoice() {
    let input = prompt("Type Rock, Paper, or Scissors");
    while (input == null) {
        input = prompt("Type Rock, Paper or Scissors");
    }
    // This part of the code should be inside the "playerChoice" function
    input = input.toLowerCase();
    let check = validateInput(input);
    console.log(input);
    return input;
}

Upvotes: 0

Related Questions