Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda