Rock Paper Scissors x99

Hi all. Would greatly appreciate some insight on why this function returns null every time. The first two if statements should and do satisfy the edge cases of invalid round number, and missing move type/value returning null, but then my main functionality does not work. If I comment out the second if statement (!type/!value) my main functionality works again.

let getRoundWinner = (round) => {
    
    if (round !== 1 && round !== 2 && round !== 3) {
        return null;
    }
    
    if (!playerOneMoveOneType || !playerOneMoveTwoType || !playerOneMoveThreeType || !playerTwoMoveOneType || !playerTwoMoveTwoType || !playerTwoMoveThreeType || !playerOneMoveOneValue || !playerOneMoveTwoValue || !playerOneMoveThreeValue || !playerTwoMoveOneValue || !playerTwoMoveTwoValue || !playerTwoMoveThreeValue) {
        return null;
    }
        
    if (round === 1) {
            if (playerOneMoveOneType === playerTwoMoveOneType && playerOneMoveOneValue === playerTwoMoveOneValue) {
                return 'Tie';
            } else if (playerOneMoveOneType === playerTwoMoveOneType && playerOneMoveOneValue >            playerTwoMoveOneValue) {
                return 'Player One';
            } else if (playerOneMoveOneType === playerTwoMoveOneType && playerOneMoveOneValue <            playerTwoMoveOneValue) {
                return 'Player Two';
            } else if (playerOneMoveOneType === 'rock' && playerTwoMoveOneType === 'scissors') {
                return 'Player One';
            } else if (playerOneMoveOneType === 'rock' && playerTwoMoveOneType === 'paper') {
                return 'Player Two';
            } else if (playerOneMoveOneType === 'paper' && playerTwoMoveOneType === 'rock') {
                return 'Player One';
            } else if (playerOneMoveOneType === 'paper' && playerTwoMoveOneType === 'scissors') {
                return 'Player Two';
            } else if (playerOneMoveOneType === 'scissors' && playerTwoMoveOneType === 'paper') {
                return 'Player One';
            } else {
                return 'Player Two';
            }
        }
}   

Hello, @mega2086963673. Welcome to the forums!

Consider the difference between these examples:

const allTrue = (a, b, c) => {
  if(a && b && c) {
    return 'All true.';
  } else {
    return 'At least one false.';
  };
};

const allFalse = (a, b, c) => {
  if(a || b || c) {
    return 'At least one true.';
  } else {
    return 'All false.';
  };
};

console.log(allTrue(true, true, true)); //All true
console.log(allTrue(true, false, true)); //At least one false.

console.log(allFalse(false, false, false)); //All false.
console.log(allFalse(false, true, false)); //At least one true.
1 Like

Hi @midlindner, thanks for the quick reply. I guess I’m still not getting it. For the Types/Values if statement I’m wanting a return null as long as anyone of those variables is undefined, which is why I’m using ‘!’. Otherwise, if they all are defined then the rest of the function should execute. I know it must just be my misunderstanding, but when I see your explanation I still don’t see how my code is wrong — but it obviously is :wink:

1 Like

Using the || operator, if any one of those evaluates to truthy you will return null. I haven’t done this particular project, so I’m not exactly sure what the outcome should be, but say for instance playerOneMoveTwoType is falsy, null is returned. Your parameter for the function is round. Presumably, round is an integer, and should be 1, 2 or 3 judging from your code. If your first if statement isn’t the culprit, the only other option for this function to return null is a falsy value being assigned to one of the variables in the second if statement.

So I think I figured out what the problem was. I broke up this problematic if statement into each respective round’s if statement (i.e. moveOneType/moveOneValue for round one) and this worked, or at least allowed me to pass the unit testing for both main functionality and edge cases. Before I was only able to pass the edge cases but not the main, or vice versa. At least your explanation confirmed it wasn’t so much a problem with my use of the logical operators as it was perhaps the way the testing responded to my particular approach. Thanks a lot for your help!

1 Like