Home > Back-end >  Javascript: False if statement always evaluates as true
Javascript: False if statement always evaluates as true

Time:02-21

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?

CodePudding user response:

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:

  • declaration of any variable in advance,
  • return the value of the array of the input,
  • use both value directly in evaluation,
  • exit early to prevent nested if ... else structures,
  • prevent using unnecessary parentheses, have a look to operator precedence, if in doubt.

function computerPlay() {
    return array[Math.floor(Math.random() * array.length)];
}

function userPlay() {
    return prompt('Choose: rock, paper or scissors');
}

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());

CodePudding user response:

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
}
  • Related