0

I'm trying to create a basic Rock, Paper, Scissors game. Because there are quite a bit of conditions for each turn, I wanted to use switch instead of if & else. My code is:

var computerChoice = Math.floor(Math.random() * 3)
var humanChoice = prompt('1 = Rock | 2 = Paper | 3 = Scissors')
        console.log(humanChoice);
        console.log(computerChoice);
    switch (computerChoice, humanChoice) {
        case 1 , 1:
            confirm('ROCK VS ROCK its a draw!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 1 , 2:
            confirm('Paper win against rock! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 1 , 3:
            confirm('Rock win against scissors! Computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 2 , 1:
            confirm('Paper win against rock! computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case 2 , 2:
            confirm('PAPER VS PAPER its a draw!!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  2 , 3:
            confirm('scissors win against paper! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 1:
            confirm('Rock win against scissors! Human won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 2:
            confirm('scissors win against Paper! Computer won!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        case  3 , 3:
            confirm('SCISSORS VS SCISSORS its a draw!');
            console.log(humanChoice);
            console.log(computerChoice);
            break;
        default:
        confirm("I don't remember telling you that " + humanChoice + " is an option.");
        break;
    }; 

The code actually jumps to the default, which is quite a bummer. What am I doing wrong? I guess it's the way I wrote the conditions in the cases, but even after searching Google, I couldn't make it work.

  • 2
    I think this is what you are looking for: https://stackoverflow.com/questions/9235152/can-i-use-a-case-switch-statement-with-two-variables – Derek Pollard Aug 01 '17 at 23:29
  • I also highly recommend checking other questions and google before posting. It took me 10 seconds to find the above link. – Derek Pollard Aug 01 '17 at 23:30
  • I guess i didn't look at the right places. i googled for hours – Aviv Ben Yosef Aug 01 '17 at 23:38
  • @Derek this solution will not work. The bitwise or operator (`|`) described in question will create overlapping cases. OP is using `computerChoice`, which can have values 0, 1 or 2 (they might not realize that it's not 1-3 like expected with cases) and then there is `humanChoice`, which can be an arbitrary string in the above code, not parsed to the number and not validated. But let's say, for the sake of argument, that both values are limited to 1, 2 or 3. In this case, 6 cases would overlap (`1 | 3 = 3`, `2 | 1 = 3`, `2 | 3 = 3`, `3 | 1 = 3`, `3 | 2 = 3`, `3 | 3 = 3`). – Marko Gresak Aug 01 '17 at 23:40
  • @AvivBenYosef based on your code, I'm assuming you're just starting out with JS. I would recommend you do to it simply, don't try to use some smart-looking code you do not understand. There is nothing wrong with doing 9 conditions. As I've mentioned in my previous comment, you have more problems in your code that need fixing. Get that right first and then try to make your code more elegant. – Marko Gresak Aug 01 '17 at 23:43
  • @MarkoGrešak Guess i should do it with if\else – Aviv Ben Yosef Aug 01 '17 at 23:44
  • 1
    @AvivBenYosef a better way to simplify your conditions would be to try and define some rules. For example, you don't need to check for all 3 types of tie; you can check once if the value of the computer is the same as what the user has chosen and just output tie. – Marko Gresak Aug 01 '17 at 23:51
  • @MarkoGrešak Thats true, I had that in my mind actually. But i wanted to exercise the concept of the "switch" because I wasn't confident with it yet. That's why I thought it might be a good idea to take a "dumber" perspective – Aviv Ben Yosef Aug 02 '17 at 02:05
  • `a, b` simply returns `b`. – melpomene Aug 02 '17 at 02:20
  • *but even after searching Google* We're still waiting for the next-generation search engine which can **think** for us. So in the meantime, we're stuck having to think for ourselves (not to mention reading the documentation). –  Aug 02 '17 at 02:20
  • @torazaburo Nah, i wouldn't like that if a search engine would think for me. As for the documentation i read them (if you refer to Stacksoverflow's docs) I guess i shouldn't have asked this question and go with another approach. – Aviv Ben Yosef Aug 02 '17 at 04:41

0 Answers0