Rewriting long IF conditional

Hi all,

I am working on the following lesson: https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/bapi-javascript-conditionals-and-functions/modules/project-rock-paper-scissors-x-99/informationals/bapi-p1-rock-paper-scissors-x99

I have to write write a function that satisfies the following assertions:

setPlayerMoves() - Main Functionality
√ a function called setPlayerMoves should exist
√ should set player one’s moves with valid inputs
√ should set player two’s moves with valid inputs

setPlayerMoves() - Edge Cases
√ should not set moves if a move type is missing
√ should not set moves if an invalid player is supplied
√ should not set moves if a move value is missing
√ should not set moves if an invalid move type is supplied
√ should not set moves if any move values are less than one
√ should not set moves if any move values are greater than ninety-nine
√ should not set moves if move values sum to more than ninety-nine

Here is my function:

function setPlayerMoves(player, moveOneType, moveOneValue, moveTwoType, moveTwoValue, moveThreeType, moveThreeValue) {

    if (moveOneType && moveTwoType && moveThreeType && moveOneValue && moveTwoValue && moveThreeValue && possibleValues.includes(moveOneType) && possibleValues.includes(moveTwoType) && possibleValues.includes(moveThreeType) && moveOneValue > 0 &&  moveTwoValue > 0 && moveThreeValue > 0 && moveOneValue < 100 && moveTwoValue < 100 && moveThreeValue < 100 && (moveOneValue + moveTwoValue + moveThreeValue) < 100) {

        if (player === "Player One") {
            playerOneMoveOneType = moveOneType;
            playerOneMoveTwoType = moveTwoType;
            playerOneMoveThreeType = moveThreeType;
            playerOneMoveOneValue = moveOneValue;
            playerOneMoveTwoValue = moveTwoValue;
            playerOneMoveThreeValue = moveThreeValue;
    
        } else if (player === "Player Two") {
            playerTwoMoveOneType = moveOneType;
            playerTwoMoveTwoType = moveTwoType;
            playerTwoMoveThreeType = moveThreeType;
            playerTwoMoveOneValue = moveOneValue;
            playerTwoMoveTwoValue = moveTwoValue;
            playerTwoMoveThreeValue = moveThreeValue;
        }
    } 
}

I was wondering if there is a better way to write the first IF condition as I have a really really long sausage of evaluations and they look a bit disorganized and unclean.

Many thanks.

1 Like

Hello @waterloo1815, welcome to the forums! I’m not sure whether this is considered better practise, but you could declare variables before the condition which include some of the conditional statement:

let truthyOrFalsey = !!(varOne && varTwo && varThree);//assuming these variables are already declared
//you could do this sort of thing-as if all three are true (or have
//truthy values), then the variable truthyOrFalsey will be `true`
if (truthyOrFalsey) {
//code
}

In the code above, the !! is used to convert the statement (varOne && varTwo && varThree) into a bool type (either true or false.) Here is some reading on how it works.


But as I said, although this may make the string of conditionals in the if statement shorter, I’m not sure if it’s good practise.
I hope this helps!

1 Like

Thank you very much for this. It does sound like good practice to me because I can declare the boolean once and then use it throughout my program.

2 Likes