2

I'm coding a basic rock paper scissors game.

the 2 functions are working fine. the issue is the winner variable is always undefined..

I don't know what to do to correct this. I want it to say who won, computer or the human(user).

function game(x, y) {
    var inputC = computerPlay();
    var inputH = humanPlay();
    var winner;

    if (inputH == 'paper' && inputC == 'scissors') {
        console.log('Computer wins with Scissors ');

        if (inputH == 'scissors' && inputC == 'rock') {
            console.log('Computer wins with rock');

            if (inputC == 'paper' && inputH == 'rock') {
                console.log('Computer wins with paper');
            }
        }

        winner = "computer";
    } else if (inputC == 'paper' && inputH == 'scissors') {
        console.log('Human wins with Scissors ');

        if (inputC == 'scissors' && inputH == 'rock') {
            console.log('Human wins with rock');

            if (inputH == 'paper' && inputC == 'rock') {
                console.log('Human wins with paper');
            }
        }
        winner = "human";
    }
    document.getElementById("text1").innerHTML = winner;
    console.log("result is: " + winner + " wins");
}

I'm sure its something minor but my god I'm all out of ideas.

2
  • 3
    your conditions are badly nested ... the only time winner will be defined is if inputH == 'paper' && inputC == 'scissors' or inputC == 'paper' && inputH == 'scissors' all other combinations will result in winner being undefined ... don't nest if's like that without understanding Commented Jul 20, 2018 at 1:12
  • thanks for the advice man, totally agree. tbh this was one of my many attempts to get around winner being undefined. if (inputH == 'paper' && inputC == 'scissors') { console.log('Computer wins with Scissors '); winner = "computer"; } else if (inputC == 'paper' && inputH == 'scissors') { console.log('Human wins with Scissors '); winner = "human"; } this is the original look( its goes through all the conditions but I shortened it here to fit) Commented Jul 20, 2018 at 15:36

2 Answers 2

3

would like this

function game() {
    var inputC = computerPlay();
    var inputH = humanPlay();
    var winner = "human";

    if (inputH === inputC) {
       winner = "nobody";
    } else if ((inputH === 'paper' && inputC === 'scissors') ||
        (inputH === 'scissors' && inputC === 'rock') ||
        (inputH === 'rock' && inputC === 'paper')
    ) {
        console.log('Computer wins with ' + inputC);
        winner = "computer";
    } else {
        console.log('Human wins with ' + inputH);
    }

    document.getElementById("text1").innerHTML = winner;
    console.log("result is: " + winner + " wins");
}
Sign up to request clarification or add additional context in comments.

4 Comments

not really ... I'd have else if (inputH == inputC) { winner = 'nobody';} then the else where human wins, to be accurate :p
that's not a flaw in your logic per se, I mean the original code had the same problem :p
thanks again. I should have caught it at the first place :-)
two equals works too. IMO, It is a good habit to be strict on comparing variables
2

it was undefined because it didn't go to either one of condition.

Another alternative answer from me

function game(x, y) {
  var inputC = computerPlay();
  var inputH = humanPlay();

  var isComputerWin = 
    (inputC == 'scissors' && inputH == 'paper') ||
    (inputC == 'rock' && inputH == 'scissors') ||
    (inputC == 'paper' && inputH == 'rock');

  var winner = isComputerWin ? 'computer' : 'human';
  var winnerWith = isComputerWin ? inputC : inputH;
  console.log(winner + " wins with " + winnerWith);

  document.getElementById("text1").innerHTML = winner;  
  console.log("result is: " + winner + " wins");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.