|
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);
codeacademyJSAPI/project-1-rock-paper-scissors-x99/project-1-rock-paper-scissors-x99/js/game-logic.js
Lines 26 to 31 in ae1b1fd
The evaluation of an expression in a boolean context (such as this) yields a boolean value.
This code can be simplified to:
... 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:
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...
As a final improvement, using the
.includes()array method would improve readability: