Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Javascript: False if statement always evaluates as true

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

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

about 4 years ago · Juan Pablo Isaza Report

0

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
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!