I am writing a simple console based rock paper scissors game as an exercise. However, the following condition always evaluates to true.
if (playerselection === computerselection) {
console.log('Draw!');
I have introduced several console.log commands as well as breakpoints to identify whether there's an issue with scope, or alteration in parameters; this does not seem to be the case. playerselection could be 'Rock', while computerselection could be 'Scissors' (both verified during debugging) and yet the condition is treated as true and the console outputs "Draw!). The entire code is seen below:
array = ['Rock', 'Paper', 'Scissors'];
computerPlay();
userPlay();
evaluation();
function computerPlay() {
computerselection = array[Math.floor(Math.random() * array.length)];
}
function userPlay() {
playerselection = prompt('Choose: Rock, Paper or Scissors', 'Choose it! NOW!!');
}
function evaluation(playerselection, computerselection) {
if (playerselection === computerselection) {
console.log('Draw!');
} else if (
(playerselection == "rock" && computerselection == "scissors") || //All possible victories
(playerselection == "paper" && computerselection == "rock") ||
(playerselection == "scissors" && computerselection == "paper")) {
console.log("You win!");
} else {
console.log("You lose)");
}
}
What is the causing the first if condition to evaluate as true every time, even if console.log(playerselection === computerselection) evaluates as false during debugging?
Some hints.
In
function evaluation(playerselection, computerselection) {
you have a declaration of two (locale) variables and you use them for the comparison.
Later, you call the function
evaluation()
without some parameters and Javascript takes a default value of undefined, like for all other declarated variables without value.
Here the code with some changes:
evaluation,if ... else structures,The data in array should be in lowercase as the input.
function computerPlay() {
return array[Math.floor(Math.random() * array.length)];
}
function userPlay() {
return (prompt('Choose: rock, paper or scissors') || '').toLowerCase();
}
function evaluation(playerselection, computerselection) {
if (playerselection === computerselection) {
console.log('Draw!');
return;
}
if (
playerselection === "rock" && computerselection === "scissors" ||
playerselection === "paper" && computerselection === "rock" ||
playerselection === "scissors" && computerselection === "paper"
) {
console.log("You win!");
return;
}
console.log("You lose");
}
const
array = ['rock', 'paper', 'scissors'];
evaluation(userPlay(), computerPlay());
Here both the variables are not in global scope and hence not accessible inside evaluate() function. This is the reason, due to which both are undefined inside evaluate() and satisfying if condition every time.
Try to define both the variables in global scope like below and remove parameters from evaluate() function declaration.
let computerselection = "";
let playerselection = "";
function evaluation() {
//code stuff
}