Skip to content

Simplify #2

@sdhunt

Description

@sdhunt

if (gameOptions.indexOf(playerMoves) > -1){
return true;
}
else{
return false;
}

The evaluation of an expression in a boolean context (such as this) yields a boolean value.

This code can be simplified to:

const ensureGameOptions = (playerMoves) =>{
    return gameOptions.indexOf(playerMoves) > -1;
};

... and, remembering that function expressions with a single parameter do not require the parentheses, and also using the "implicit return" form, can also drop the braces, can be simplified further:

const ensureGameOptions = playerMoves => gameOptions.indexOf(playerMoves) > -1;

However, I would also suggest renaming the function and variables to make it clearer as to what they are. Note also, you can assign the array to a constant, since it should never change during the run of the program...

const moveTypes = ["rock", "paper", "scissors"];

const isValidType = type => moveTypes.indexOf(type) > -1;

As a final improvement, using the .includes() array method would improve readability:

const isValidType = type => moveTypes.includes(type);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions