Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda