samedi 3 avril 2021

unpredictable result of "else if" statement within a function in JS [duplicate]

Working on a JavaScript Rock/Paper/Scissors game for a foundations Odin Project assignment that runs in the console. I made a function to compare playerSelection and computerSelection and determine win, lose, draw, or the playerSelection was not valid. The problem is, if for the playerSelection I input nonsense (i.e. asdf) It will correctly identify the input as invalid about 80% of the time, but will (seemingly) randomly return the result as a draw.

const playRound = (playerSelection, computerSelection) => {
// if and else if for win/lose are here.
else if 
        ((playerSelection.toLowerCase() && computerSelection) === ('rock' || 'paper' || 'scissors')) {
            return `It's a draw\nYou chose ${playerSelection}\nComputer chose ${computerSelection}`;
    } else return `Looks like your input was invalid\n${playerSelection}`
};

Why the inconsistent return?

Full code: https://github.com/justbkoz/rockPaperScissors/blob/main/rockPaperScissors.js

Aucun commentaire:

Enregistrer un commentaire