More efficient choice comparison for Rock Paper Scissors











up vote
9
down vote

favorite












This is an ongoing school project that I would like to improve. The point is to make the code as efficient (or short) as possible. I would like to reduce it by finding an alternative to all the else ifs when comparing the computer's choice with the user's choice.



Here is the code:



let weapons = ["Rock", "Paper", "Scissors"];
let random = Math.floor(Math.random()*3);
let chosenOne = weapons[random];

let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?"
+ 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number
2.");

if (rps === "1") {
alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " -
Paper beats the rock" + 'n' + " - The scissors cut the paper");

let weapon = prompt("Make your choice:" + 'n' + "Rock, Paper, Scissors");
weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
alert("You chose: " + weapon + 'n' + "The computer chose: " +
chosenOne);
if (weapon === chosenOne) {
alert("It's a tie! Try again to win!");
} else if (weapon === "Rock" && chosenOne === "Paper") {
alert("You lost! Paper beats the rock.");
} else if (weapon === "Paper" && chosenOne === "Scissors") {
alert("You lost! The scissors cut the paper.");
} else if (weapon === "Scissors" && chosenOne === "Rock") {
alert("You lost! The rock beats the scissors.");
} else if (weapon === "Scissors" && chosenOne === "Paper") {
alert("You won! Scissors cut the paper.");
} else if (weapon === "Paper" && chosenOne === "Rock") {
alert("You won! Paper beats the rock.");
} else if (weapon === "Rock" && chosenOne === "Scissors") {
alert("You won! The rock beats the scissors.");
}
} else if (rps === "2") {
alert("Thanks for visiting! See you later.");
} else if (rps !== "1" || rps !== "2") {
alert("Invalid option. Closing game.");
}


I have thought about using switch statements, but since we are still beginners, I haven't grasped the subject fully. Any help is appreciated.










share|improve this question


























    up vote
    9
    down vote

    favorite












    This is an ongoing school project that I would like to improve. The point is to make the code as efficient (or short) as possible. I would like to reduce it by finding an alternative to all the else ifs when comparing the computer's choice with the user's choice.



    Here is the code:



    let weapons = ["Rock", "Paper", "Scissors"];
    let random = Math.floor(Math.random()*3);
    let chosenOne = weapons[random];

    let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?"
    + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number
    2.");

    if (rps === "1") {
    alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " -
    Paper beats the rock" + 'n' + " - The scissors cut the paper");

    let weapon = prompt("Make your choice:" + 'n' + "Rock, Paper, Scissors");
    weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
    alert("You chose: " + weapon + 'n' + "The computer chose: " +
    chosenOne);
    if (weapon === chosenOne) {
    alert("It's a tie! Try again to win!");
    } else if (weapon === "Rock" && chosenOne === "Paper") {
    alert("You lost! Paper beats the rock.");
    } else if (weapon === "Paper" && chosenOne === "Scissors") {
    alert("You lost! The scissors cut the paper.");
    } else if (weapon === "Scissors" && chosenOne === "Rock") {
    alert("You lost! The rock beats the scissors.");
    } else if (weapon === "Scissors" && chosenOne === "Paper") {
    alert("You won! Scissors cut the paper.");
    } else if (weapon === "Paper" && chosenOne === "Rock") {
    alert("You won! Paper beats the rock.");
    } else if (weapon === "Rock" && chosenOne === "Scissors") {
    alert("You won! The rock beats the scissors.");
    }
    } else if (rps === "2") {
    alert("Thanks for visiting! See you later.");
    } else if (rps !== "1" || rps !== "2") {
    alert("Invalid option. Closing game.");
    }


    I have thought about using switch statements, but since we are still beginners, I haven't grasped the subject fully. Any help is appreciated.










    share|improve this question
























      up vote
      9
      down vote

      favorite









      up vote
      9
      down vote

      favorite











      This is an ongoing school project that I would like to improve. The point is to make the code as efficient (or short) as possible. I would like to reduce it by finding an alternative to all the else ifs when comparing the computer's choice with the user's choice.



      Here is the code:



      let weapons = ["Rock", "Paper", "Scissors"];
      let random = Math.floor(Math.random()*3);
      let chosenOne = weapons[random];

      let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?"
      + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number
      2.");

      if (rps === "1") {
      alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " -
      Paper beats the rock" + 'n' + " - The scissors cut the paper");

      let weapon = prompt("Make your choice:" + 'n' + "Rock, Paper, Scissors");
      weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
      alert("You chose: " + weapon + 'n' + "The computer chose: " +
      chosenOne);
      if (weapon === chosenOne) {
      alert("It's a tie! Try again to win!");
      } else if (weapon === "Rock" && chosenOne === "Paper") {
      alert("You lost! Paper beats the rock.");
      } else if (weapon === "Paper" && chosenOne === "Scissors") {
      alert("You lost! The scissors cut the paper.");
      } else if (weapon === "Scissors" && chosenOne === "Rock") {
      alert("You lost! The rock beats the scissors.");
      } else if (weapon === "Scissors" && chosenOne === "Paper") {
      alert("You won! Scissors cut the paper.");
      } else if (weapon === "Paper" && chosenOne === "Rock") {
      alert("You won! Paper beats the rock.");
      } else if (weapon === "Rock" && chosenOne === "Scissors") {
      alert("You won! The rock beats the scissors.");
      }
      } else if (rps === "2") {
      alert("Thanks for visiting! See you later.");
      } else if (rps !== "1" || rps !== "2") {
      alert("Invalid option. Closing game.");
      }


      I have thought about using switch statements, but since we are still beginners, I haven't grasped the subject fully. Any help is appreciated.










      share|improve this question













      This is an ongoing school project that I would like to improve. The point is to make the code as efficient (or short) as possible. I would like to reduce it by finding an alternative to all the else ifs when comparing the computer's choice with the user's choice.



      Here is the code:



      let weapons = ["Rock", "Paper", "Scissors"];
      let random = Math.floor(Math.random()*3);
      let chosenOne = weapons[random];

      let rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?"
      + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number
      2.");

      if (rps === "1") {
      alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " -
      Paper beats the rock" + 'n' + " - The scissors cut the paper");

      let weapon = prompt("Make your choice:" + 'n' + "Rock, Paper, Scissors");
      weapon = weapon.charAt(0).toUpperCase() + weapon.slice(1).toLowerCase();
      alert("You chose: " + weapon + 'n' + "The computer chose: " +
      chosenOne);
      if (weapon === chosenOne) {
      alert("It's a tie! Try again to win!");
      } else if (weapon === "Rock" && chosenOne === "Paper") {
      alert("You lost! Paper beats the rock.");
      } else if (weapon === "Paper" && chosenOne === "Scissors") {
      alert("You lost! The scissors cut the paper.");
      } else if (weapon === "Scissors" && chosenOne === "Rock") {
      alert("You lost! The rock beats the scissors.");
      } else if (weapon === "Scissors" && chosenOne === "Paper") {
      alert("You won! Scissors cut the paper.");
      } else if (weapon === "Paper" && chosenOne === "Rock") {
      alert("You won! Paper beats the rock.");
      } else if (weapon === "Rock" && chosenOne === "Scissors") {
      alert("You won! The rock beats the scissors.");
      }
      } else if (rps === "2") {
      alert("Thanks for visiting! See you later.");
      } else if (rps !== "1" || rps !== "2") {
      alert("Invalid option. Closing game.");
      }


      I have thought about using switch statements, but since we are still beginners, I haven't grasped the subject fully. Any help is appreciated.







      javascript






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 7 hours ago









      TBG

      462




      462
























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          10
          down vote













          You can define an object that define if your move is weak or strong against another. Example:



          const myChoice = 'Rock'
          const enemyChoice = 'Scissors'

          const weapons = {
          Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
          Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
          Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
          }

          if (weapons[myChoice].strongTo === enemyChoice) {
          // I won
          return;
          }

          if (weapons[myChoice].weakTo === enemyChoice) {
          // I Lost
          return;
          }

          // tie





          share|improve this answer




























            up vote
            0
            down vote













            Switch statements goes like that.
            Try to look at the e.g i gave you and try to understand the flow of the code.



            Best way to learn is with your hands!
            Random switch eg:



               switch(expression) {
            case x:
            code block
            break; // after every statement e.g: (console.log) you need to use
            "break"
            case y:
            code block
            break;
            default:
            code block


            }



            Your code:



            switch (weapon) {
            case chosenOne:
            return console.log(alert("It's a tie! Try again to win!"));
            break;
            case "Rock":
            case "Paper":
            alert("You lost! Paper beats the rock.");
            break;
            case "Paper":
            case "Scissors":
            console.log(alert("You lost! The scissors cut the paper."))
            break;
            case "Scissors":
            case "Rock":
            console.log(alert("You lost! The rock beats the scissors."))
            break;
            case "Scissors":
            case "Paper" :
            alert("You won! Scissors cut the paper.");
            break;
            case "Paper":
            case "Rock":
            console.log(alert("You won! Paper beats the rock."))
            break;
            case "Rock":
            case "Scissors":
            alert("You won! The rock beats the scissors.");
            default:
            return "somthing went wrong"
            break;





            share|improve this answer




























              up vote
              0
              down vote













              You can also use an array to check the winner. Order the array so that the winner is always on the right side. Then compare if the machine's choise is the one next to user's choise, like so:






              var weapons = ['paper', 'scissors', 'rock'],
              user = 'scissors',
              machine = 'paper',
              uIdx = weapons.indexOf(user),
              mIdx = weapons.indexOf(machine),
              winner;
              if (uIdx !== mIdx) {
              winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
              } else {
              winner = 'tie';
              }

              console.log(winner);





              A fiddle to play with.



              The modulo operator makes the magic at the end of the array. If user has chosen "rock", the next to it would be undefined, but the modulo operator of 3 % 3 returns 0, hence "paper" is compared to "rock".






              share|improve this answer




























                up vote
                0
                down vote













                Maybe not the clearest code, but it's instructional:



                const battle = ["Rock", "Paper", "Scissors"].map(
                (weapon, index, array) => [weapon, enemy => {
                const res = index - array.findIndex(x => x === enemy)
                return !!(res%2)? res : res * -1
                }]
                ).reduce((accumulator, current) => {accumulator[current[0]] = current[1]; return accumulator}, {})

                const output = mine => enemy => {
                const result = battle[mine](enemy)
                return result > 0 ? `You won! ${mine} beats ${enemy}`
                : result < 0 ? `You lost! ${enemy} beats ${mine}`
                : "It's a tie! Try again to win"
                }

                console.log(output("Rock")("Paper"))


                Take it as an exercise to learn about arrays, objects, arrow functions, ternary operators, currying, string templates and more.






                share|improve this answer




























                  up vote
                  0
                  down vote













                  I removed some of your variables and combined some, just to make it shorter. I also got rid of the bulk of the if/else since it's not really needed here. For more info on how a switch works, check out https://javascript.info/switch.



                  I also changed up your choices so that you can add multiple win or loss conditions for each choice, in case you wanted to upgrade to Rock,Paper,Scissors,Lizard,Spock ( https://www.youtube.com/watch?v=cSLeBKT7-s ).



                  // Set up our various choices, how they rank, and their action (can also be array if desired).
                  const choices = {
                  Rock : { win:["Scissors"] , action:"beats" } ,
                  Paper : { win:["Rock"] , action:"beats" } ,
                  Scissors : { win:["Paper"] , action:"cuts" } ,
                  Spock : { win:["Rock","Scissors"] , action:"beats" }
                  } ;

                  // Use the keys in choices as our selectable items.
                  const weapons = Object.keys(choices) ;

                  // Our basic intro.
                  const rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number 2.");

                  // Set the computer choice.
                  const chosenOne = weapons[Math.floor(Math.random()*3)];

                  // This is an example of your switch.
                  switch (rps) {
                  case "1" : // Since we used text input, we have to evaluate for a text "number".
                  alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " - Paper beats the rock" + 'n' + " - The scissors cut the paper");

                  // Make your choice.
                  let weapon = prompt("Make your choice:" + 'n' + weapons, "");
                  // Is our choice valid?
                  if ( !weapons.includes(weapon) ) {
                  alert("Invalid choice. Closing Game."); break;
                  } else {
                  alert("You chose: " + weapon + 'n' + "The computer chose: " + chosenOne);
                  }
                  // Did I win?
                  alert( compareRPS(weapon,chosenOne) ) ;
                  break ; // This will break out of the switch. Otherwise will fall through to next case.

                  case "2":
                  alert("Thanks for visiting! See you later.");
                  break ;

                  default :
                  alert("Invalid option. Closing game.");
                  // No break needed here since this is the end of the switch.
                  }

                  // I broke the check-you-vs-cpu functionality out into its own function.
                  function compareRPS(youC,cpuC) {
                  if ( youC === cpuC ) {
                  return "It's a tie! Try again to win." ;
                  }
                  if (choices[youC].win.includes(cpuC)) {
                  return "You won! " + youC + " " + choices[youC].action + " " + cpuC + "." ;
                  } else {
                  return "You lost! " + cpuC + " " + choices[cpuC].action + " " + youC + "." ;
                  }
                  }


                  NOTE: I also switch between const and let. See https://codeburst.io/part-2-var-vs-const-vs-let-69ea73fe76c1 for differences. I mostly use const to indicate a variable I won't change and let to be one that I can (within its proper scope). There's also var, but I didn't need it here.






                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53730900%2fmore-efficient-choice-comparison-for-rock-paper-scissors%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    5 Answers
                    5






                    active

                    oldest

                    votes








                    5 Answers
                    5






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    10
                    down vote













                    You can define an object that define if your move is weak or strong against another. Example:



                    const myChoice = 'Rock'
                    const enemyChoice = 'Scissors'

                    const weapons = {
                    Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
                    Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
                    Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
                    }

                    if (weapons[myChoice].strongTo === enemyChoice) {
                    // I won
                    return;
                    }

                    if (weapons[myChoice].weakTo === enemyChoice) {
                    // I Lost
                    return;
                    }

                    // tie





                    share|improve this answer

























                      up vote
                      10
                      down vote













                      You can define an object that define if your move is weak or strong against another. Example:



                      const myChoice = 'Rock'
                      const enemyChoice = 'Scissors'

                      const weapons = {
                      Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
                      Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
                      Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
                      }

                      if (weapons[myChoice].strongTo === enemyChoice) {
                      // I won
                      return;
                      }

                      if (weapons[myChoice].weakTo === enemyChoice) {
                      // I Lost
                      return;
                      }

                      // tie





                      share|improve this answer























                        up vote
                        10
                        down vote










                        up vote
                        10
                        down vote









                        You can define an object that define if your move is weak or strong against another. Example:



                        const myChoice = 'Rock'
                        const enemyChoice = 'Scissors'

                        const weapons = {
                        Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
                        Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
                        Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
                        }

                        if (weapons[myChoice].strongTo === enemyChoice) {
                        // I won
                        return;
                        }

                        if (weapons[myChoice].weakTo === enemyChoice) {
                        // I Lost
                        return;
                        }

                        // tie





                        share|improve this answer












                        You can define an object that define if your move is weak or strong against another. Example:



                        const myChoice = 'Rock'
                        const enemyChoice = 'Scissors'

                        const weapons = {
                        Rock: {weakTo: 'Paper', strongTo: 'Scissors'},
                        Paper: {weakTo: 'Scissors', strongTo: 'Rock'},
                        Scissors: {weakTo: 'Rock', strongTo: 'Paper'}
                        }

                        if (weapons[myChoice].strongTo === enemyChoice) {
                        // I won
                        return;
                        }

                        if (weapons[myChoice].weakTo === enemyChoice) {
                        // I Lost
                        return;
                        }

                        // tie






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 7 hours ago









                        Federkun

                        21.9k74366




                        21.9k74366
























                            up vote
                            0
                            down vote













                            Switch statements goes like that.
                            Try to look at the e.g i gave you and try to understand the flow of the code.



                            Best way to learn is with your hands!
                            Random switch eg:



                               switch(expression) {
                            case x:
                            code block
                            break; // after every statement e.g: (console.log) you need to use
                            "break"
                            case y:
                            code block
                            break;
                            default:
                            code block


                            }



                            Your code:



                            switch (weapon) {
                            case chosenOne:
                            return console.log(alert("It's a tie! Try again to win!"));
                            break;
                            case "Rock":
                            case "Paper":
                            alert("You lost! Paper beats the rock.");
                            break;
                            case "Paper":
                            case "Scissors":
                            console.log(alert("You lost! The scissors cut the paper."))
                            break;
                            case "Scissors":
                            case "Rock":
                            console.log(alert("You lost! The rock beats the scissors."))
                            break;
                            case "Scissors":
                            case "Paper" :
                            alert("You won! Scissors cut the paper.");
                            break;
                            case "Paper":
                            case "Rock":
                            console.log(alert("You won! Paper beats the rock."))
                            break;
                            case "Rock":
                            case "Scissors":
                            alert("You won! The rock beats the scissors.");
                            default:
                            return "somthing went wrong"
                            break;





                            share|improve this answer

























                              up vote
                              0
                              down vote













                              Switch statements goes like that.
                              Try to look at the e.g i gave you and try to understand the flow of the code.



                              Best way to learn is with your hands!
                              Random switch eg:



                                 switch(expression) {
                              case x:
                              code block
                              break; // after every statement e.g: (console.log) you need to use
                              "break"
                              case y:
                              code block
                              break;
                              default:
                              code block


                              }



                              Your code:



                              switch (weapon) {
                              case chosenOne:
                              return console.log(alert("It's a tie! Try again to win!"));
                              break;
                              case "Rock":
                              case "Paper":
                              alert("You lost! Paper beats the rock.");
                              break;
                              case "Paper":
                              case "Scissors":
                              console.log(alert("You lost! The scissors cut the paper."))
                              break;
                              case "Scissors":
                              case "Rock":
                              console.log(alert("You lost! The rock beats the scissors."))
                              break;
                              case "Scissors":
                              case "Paper" :
                              alert("You won! Scissors cut the paper.");
                              break;
                              case "Paper":
                              case "Rock":
                              console.log(alert("You won! Paper beats the rock."))
                              break;
                              case "Rock":
                              case "Scissors":
                              alert("You won! The rock beats the scissors.");
                              default:
                              return "somthing went wrong"
                              break;





                              share|improve this answer























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Switch statements goes like that.
                                Try to look at the e.g i gave you and try to understand the flow of the code.



                                Best way to learn is with your hands!
                                Random switch eg:



                                   switch(expression) {
                                case x:
                                code block
                                break; // after every statement e.g: (console.log) you need to use
                                "break"
                                case y:
                                code block
                                break;
                                default:
                                code block


                                }



                                Your code:



                                switch (weapon) {
                                case chosenOne:
                                return console.log(alert("It's a tie! Try again to win!"));
                                break;
                                case "Rock":
                                case "Paper":
                                alert("You lost! Paper beats the rock.");
                                break;
                                case "Paper":
                                case "Scissors":
                                console.log(alert("You lost! The scissors cut the paper."))
                                break;
                                case "Scissors":
                                case "Rock":
                                console.log(alert("You lost! The rock beats the scissors."))
                                break;
                                case "Scissors":
                                case "Paper" :
                                alert("You won! Scissors cut the paper.");
                                break;
                                case "Paper":
                                case "Rock":
                                console.log(alert("You won! Paper beats the rock."))
                                break;
                                case "Rock":
                                case "Scissors":
                                alert("You won! The rock beats the scissors.");
                                default:
                                return "somthing went wrong"
                                break;





                                share|improve this answer












                                Switch statements goes like that.
                                Try to look at the e.g i gave you and try to understand the flow of the code.



                                Best way to learn is with your hands!
                                Random switch eg:



                                   switch(expression) {
                                case x:
                                code block
                                break; // after every statement e.g: (console.log) you need to use
                                "break"
                                case y:
                                code block
                                break;
                                default:
                                code block


                                }



                                Your code:



                                switch (weapon) {
                                case chosenOne:
                                return console.log(alert("It's a tie! Try again to win!"));
                                break;
                                case "Rock":
                                case "Paper":
                                alert("You lost! Paper beats the rock.");
                                break;
                                case "Paper":
                                case "Scissors":
                                console.log(alert("You lost! The scissors cut the paper."))
                                break;
                                case "Scissors":
                                case "Rock":
                                console.log(alert("You lost! The rock beats the scissors."))
                                break;
                                case "Scissors":
                                case "Paper" :
                                alert("You won! Scissors cut the paper.");
                                break;
                                case "Paper":
                                case "Rock":
                                console.log(alert("You won! Paper beats the rock."))
                                break;
                                case "Rock":
                                case "Scissors":
                                alert("You won! The rock beats the scissors.");
                                default:
                                return "somthing went wrong"
                                break;






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered 7 hours ago









                                Shibex

                                113




                                113






















                                    up vote
                                    0
                                    down vote













                                    You can also use an array to check the winner. Order the array so that the winner is always on the right side. Then compare if the machine's choise is the one next to user's choise, like so:






                                    var weapons = ['paper', 'scissors', 'rock'],
                                    user = 'scissors',
                                    machine = 'paper',
                                    uIdx = weapons.indexOf(user),
                                    mIdx = weapons.indexOf(machine),
                                    winner;
                                    if (uIdx !== mIdx) {
                                    winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                    } else {
                                    winner = 'tie';
                                    }

                                    console.log(winner);





                                    A fiddle to play with.



                                    The modulo operator makes the magic at the end of the array. If user has chosen "rock", the next to it would be undefined, but the modulo operator of 3 % 3 returns 0, hence "paper" is compared to "rock".






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      You can also use an array to check the winner. Order the array so that the winner is always on the right side. Then compare if the machine's choise is the one next to user's choise, like so:






                                      var weapons = ['paper', 'scissors', 'rock'],
                                      user = 'scissors',
                                      machine = 'paper',
                                      uIdx = weapons.indexOf(user),
                                      mIdx = weapons.indexOf(machine),
                                      winner;
                                      if (uIdx !== mIdx) {
                                      winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                      } else {
                                      winner = 'tie';
                                      }

                                      console.log(winner);





                                      A fiddle to play with.



                                      The modulo operator makes the magic at the end of the array. If user has chosen "rock", the next to it would be undefined, but the modulo operator of 3 % 3 returns 0, hence "paper" is compared to "rock".






                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        You can also use an array to check the winner. Order the array so that the winner is always on the right side. Then compare if the machine's choise is the one next to user's choise, like so:






                                        var weapons = ['paper', 'scissors', 'rock'],
                                        user = 'scissors',
                                        machine = 'paper',
                                        uIdx = weapons.indexOf(user),
                                        mIdx = weapons.indexOf(machine),
                                        winner;
                                        if (uIdx !== mIdx) {
                                        winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                        } else {
                                        winner = 'tie';
                                        }

                                        console.log(winner);





                                        A fiddle to play with.



                                        The modulo operator makes the magic at the end of the array. If user has chosen "rock", the next to it would be undefined, but the modulo operator of 3 % 3 returns 0, hence "paper" is compared to "rock".






                                        share|improve this answer












                                        You can also use an array to check the winner. Order the array so that the winner is always on the right side. Then compare if the machine's choise is the one next to user's choise, like so:






                                        var weapons = ['paper', 'scissors', 'rock'],
                                        user = 'scissors',
                                        machine = 'paper',
                                        uIdx = weapons.indexOf(user),
                                        mIdx = weapons.indexOf(machine),
                                        winner;
                                        if (uIdx !== mIdx) {
                                        winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                        } else {
                                        winner = 'tie';
                                        }

                                        console.log(winner);





                                        A fiddle to play with.



                                        The modulo operator makes the magic at the end of the array. If user has chosen "rock", the next to it would be undefined, but the modulo operator of 3 % 3 returns 0, hence "paper" is compared to "rock".






                                        var weapons = ['paper', 'scissors', 'rock'],
                                        user = 'scissors',
                                        machine = 'paper',
                                        uIdx = weapons.indexOf(user),
                                        mIdx = weapons.indexOf(machine),
                                        winner;
                                        if (uIdx !== mIdx) {
                                        winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                        } else {
                                        winner = 'tie';
                                        }

                                        console.log(winner);





                                        var weapons = ['paper', 'scissors', 'rock'],
                                        user = 'scissors',
                                        machine = 'paper',
                                        uIdx = weapons.indexOf(user),
                                        mIdx = weapons.indexOf(machine),
                                        winner;
                                        if (uIdx !== mIdx) {
                                        winner = (mIdx === (uIdx + 1) % 3) ? 'machine' : 'user';
                                        } else {
                                        winner = 'tie';
                                        }

                                        console.log(winner);






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 6 hours ago









                                        Teemu

                                        18.2k63471




                                        18.2k63471






















                                            up vote
                                            0
                                            down vote













                                            Maybe not the clearest code, but it's instructional:



                                            const battle = ["Rock", "Paper", "Scissors"].map(
                                            (weapon, index, array) => [weapon, enemy => {
                                            const res = index - array.findIndex(x => x === enemy)
                                            return !!(res%2)? res : res * -1
                                            }]
                                            ).reduce((accumulator, current) => {accumulator[current[0]] = current[1]; return accumulator}, {})

                                            const output = mine => enemy => {
                                            const result = battle[mine](enemy)
                                            return result > 0 ? `You won! ${mine} beats ${enemy}`
                                            : result < 0 ? `You lost! ${enemy} beats ${mine}`
                                            : "It's a tie! Try again to win"
                                            }

                                            console.log(output("Rock")("Paper"))


                                            Take it as an exercise to learn about arrays, objects, arrow functions, ternary operators, currying, string templates and more.






                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote













                                              Maybe not the clearest code, but it's instructional:



                                              const battle = ["Rock", "Paper", "Scissors"].map(
                                              (weapon, index, array) => [weapon, enemy => {
                                              const res = index - array.findIndex(x => x === enemy)
                                              return !!(res%2)? res : res * -1
                                              }]
                                              ).reduce((accumulator, current) => {accumulator[current[0]] = current[1]; return accumulator}, {})

                                              const output = mine => enemy => {
                                              const result = battle[mine](enemy)
                                              return result > 0 ? `You won! ${mine} beats ${enemy}`
                                              : result < 0 ? `You lost! ${enemy} beats ${mine}`
                                              : "It's a tie! Try again to win"
                                              }

                                              console.log(output("Rock")("Paper"))


                                              Take it as an exercise to learn about arrays, objects, arrow functions, ternary operators, currying, string templates and more.






                                              share|improve this answer























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Maybe not the clearest code, but it's instructional:



                                                const battle = ["Rock", "Paper", "Scissors"].map(
                                                (weapon, index, array) => [weapon, enemy => {
                                                const res = index - array.findIndex(x => x === enemy)
                                                return !!(res%2)? res : res * -1
                                                }]
                                                ).reduce((accumulator, current) => {accumulator[current[0]] = current[1]; return accumulator}, {})

                                                const output = mine => enemy => {
                                                const result = battle[mine](enemy)
                                                return result > 0 ? `You won! ${mine} beats ${enemy}`
                                                : result < 0 ? `You lost! ${enemy} beats ${mine}`
                                                : "It's a tie! Try again to win"
                                                }

                                                console.log(output("Rock")("Paper"))


                                                Take it as an exercise to learn about arrays, objects, arrow functions, ternary operators, currying, string templates and more.






                                                share|improve this answer












                                                Maybe not the clearest code, but it's instructional:



                                                const battle = ["Rock", "Paper", "Scissors"].map(
                                                (weapon, index, array) => [weapon, enemy => {
                                                const res = index - array.findIndex(x => x === enemy)
                                                return !!(res%2)? res : res * -1
                                                }]
                                                ).reduce((accumulator, current) => {accumulator[current[0]] = current[1]; return accumulator}, {})

                                                const output = mine => enemy => {
                                                const result = battle[mine](enemy)
                                                return result > 0 ? `You won! ${mine} beats ${enemy}`
                                                : result < 0 ? `You lost! ${enemy} beats ${mine}`
                                                : "It's a tie! Try again to win"
                                                }

                                                console.log(output("Rock")("Paper"))


                                                Take it as an exercise to learn about arrays, objects, arrow functions, ternary operators, currying, string templates and more.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 5 hours ago









                                                jorbuedo

                                                4769




                                                4769






















                                                    up vote
                                                    0
                                                    down vote













                                                    I removed some of your variables and combined some, just to make it shorter. I also got rid of the bulk of the if/else since it's not really needed here. For more info on how a switch works, check out https://javascript.info/switch.



                                                    I also changed up your choices so that you can add multiple win or loss conditions for each choice, in case you wanted to upgrade to Rock,Paper,Scissors,Lizard,Spock ( https://www.youtube.com/watch?v=cSLeBKT7-s ).



                                                    // Set up our various choices, how they rank, and their action (can also be array if desired).
                                                    const choices = {
                                                    Rock : { win:["Scissors"] , action:"beats" } ,
                                                    Paper : { win:["Rock"] , action:"beats" } ,
                                                    Scissors : { win:["Paper"] , action:"cuts" } ,
                                                    Spock : { win:["Rock","Scissors"] , action:"beats" }
                                                    } ;

                                                    // Use the keys in choices as our selectable items.
                                                    const weapons = Object.keys(choices) ;

                                                    // Our basic intro.
                                                    const rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number 2.");

                                                    // Set the computer choice.
                                                    const chosenOne = weapons[Math.floor(Math.random()*3)];

                                                    // This is an example of your switch.
                                                    switch (rps) {
                                                    case "1" : // Since we used text input, we have to evaluate for a text "number".
                                                    alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " - Paper beats the rock" + 'n' + " - The scissors cut the paper");

                                                    // Make your choice.
                                                    let weapon = prompt("Make your choice:" + 'n' + weapons, "");
                                                    // Is our choice valid?
                                                    if ( !weapons.includes(weapon) ) {
                                                    alert("Invalid choice. Closing Game."); break;
                                                    } else {
                                                    alert("You chose: " + weapon + 'n' + "The computer chose: " + chosenOne);
                                                    }
                                                    // Did I win?
                                                    alert( compareRPS(weapon,chosenOne) ) ;
                                                    break ; // This will break out of the switch. Otherwise will fall through to next case.

                                                    case "2":
                                                    alert("Thanks for visiting! See you later.");
                                                    break ;

                                                    default :
                                                    alert("Invalid option. Closing game.");
                                                    // No break needed here since this is the end of the switch.
                                                    }

                                                    // I broke the check-you-vs-cpu functionality out into its own function.
                                                    function compareRPS(youC,cpuC) {
                                                    if ( youC === cpuC ) {
                                                    return "It's a tie! Try again to win." ;
                                                    }
                                                    if (choices[youC].win.includes(cpuC)) {
                                                    return "You won! " + youC + " " + choices[youC].action + " " + cpuC + "." ;
                                                    } else {
                                                    return "You lost! " + cpuC + " " + choices[cpuC].action + " " + youC + "." ;
                                                    }
                                                    }


                                                    NOTE: I also switch between const and let. See https://codeburst.io/part-2-var-vs-const-vs-let-69ea73fe76c1 for differences. I mostly use const to indicate a variable I won't change and let to be one that I can (within its proper scope). There's also var, but I didn't need it here.






                                                    share|improve this answer



























                                                      up vote
                                                      0
                                                      down vote













                                                      I removed some of your variables and combined some, just to make it shorter. I also got rid of the bulk of the if/else since it's not really needed here. For more info on how a switch works, check out https://javascript.info/switch.



                                                      I also changed up your choices so that you can add multiple win or loss conditions for each choice, in case you wanted to upgrade to Rock,Paper,Scissors,Lizard,Spock ( https://www.youtube.com/watch?v=cSLeBKT7-s ).



                                                      // Set up our various choices, how they rank, and their action (can also be array if desired).
                                                      const choices = {
                                                      Rock : { win:["Scissors"] , action:"beats" } ,
                                                      Paper : { win:["Rock"] , action:"beats" } ,
                                                      Scissors : { win:["Paper"] , action:"cuts" } ,
                                                      Spock : { win:["Rock","Scissors"] , action:"beats" }
                                                      } ;

                                                      // Use the keys in choices as our selectable items.
                                                      const weapons = Object.keys(choices) ;

                                                      // Our basic intro.
                                                      const rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number 2.");

                                                      // Set the computer choice.
                                                      const chosenOne = weapons[Math.floor(Math.random()*3)];

                                                      // This is an example of your switch.
                                                      switch (rps) {
                                                      case "1" : // Since we used text input, we have to evaluate for a text "number".
                                                      alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " - Paper beats the rock" + 'n' + " - The scissors cut the paper");

                                                      // Make your choice.
                                                      let weapon = prompt("Make your choice:" + 'n' + weapons, "");
                                                      // Is our choice valid?
                                                      if ( !weapons.includes(weapon) ) {
                                                      alert("Invalid choice. Closing Game."); break;
                                                      } else {
                                                      alert("You chose: " + weapon + 'n' + "The computer chose: " + chosenOne);
                                                      }
                                                      // Did I win?
                                                      alert( compareRPS(weapon,chosenOne) ) ;
                                                      break ; // This will break out of the switch. Otherwise will fall through to next case.

                                                      case "2":
                                                      alert("Thanks for visiting! See you later.");
                                                      break ;

                                                      default :
                                                      alert("Invalid option. Closing game.");
                                                      // No break needed here since this is the end of the switch.
                                                      }

                                                      // I broke the check-you-vs-cpu functionality out into its own function.
                                                      function compareRPS(youC,cpuC) {
                                                      if ( youC === cpuC ) {
                                                      return "It's a tie! Try again to win." ;
                                                      }
                                                      if (choices[youC].win.includes(cpuC)) {
                                                      return "You won! " + youC + " " + choices[youC].action + " " + cpuC + "." ;
                                                      } else {
                                                      return "You lost! " + cpuC + " " + choices[cpuC].action + " " + youC + "." ;
                                                      }
                                                      }


                                                      NOTE: I also switch between const and let. See https://codeburst.io/part-2-var-vs-const-vs-let-69ea73fe76c1 for differences. I mostly use const to indicate a variable I won't change and let to be one that I can (within its proper scope). There's also var, but I didn't need it here.






                                                      share|improve this answer

























                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote









                                                        I removed some of your variables and combined some, just to make it shorter. I also got rid of the bulk of the if/else since it's not really needed here. For more info on how a switch works, check out https://javascript.info/switch.



                                                        I also changed up your choices so that you can add multiple win or loss conditions for each choice, in case you wanted to upgrade to Rock,Paper,Scissors,Lizard,Spock ( https://www.youtube.com/watch?v=cSLeBKT7-s ).



                                                        // Set up our various choices, how they rank, and their action (can also be array if desired).
                                                        const choices = {
                                                        Rock : { win:["Scissors"] , action:"beats" } ,
                                                        Paper : { win:["Rock"] , action:"beats" } ,
                                                        Scissors : { win:["Paper"] , action:"cuts" } ,
                                                        Spock : { win:["Rock","Scissors"] , action:"beats" }
                                                        } ;

                                                        // Use the keys in choices as our selectable items.
                                                        const weapons = Object.keys(choices) ;

                                                        // Our basic intro.
                                                        const rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number 2.");

                                                        // Set the computer choice.
                                                        const chosenOne = weapons[Math.floor(Math.random()*3)];

                                                        // This is an example of your switch.
                                                        switch (rps) {
                                                        case "1" : // Since we used text input, we have to evaluate for a text "number".
                                                        alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " - Paper beats the rock" + 'n' + " - The scissors cut the paper");

                                                        // Make your choice.
                                                        let weapon = prompt("Make your choice:" + 'n' + weapons, "");
                                                        // Is our choice valid?
                                                        if ( !weapons.includes(weapon) ) {
                                                        alert("Invalid choice. Closing Game."); break;
                                                        } else {
                                                        alert("You chose: " + weapon + 'n' + "The computer chose: " + chosenOne);
                                                        }
                                                        // Did I win?
                                                        alert( compareRPS(weapon,chosenOne) ) ;
                                                        break ; // This will break out of the switch. Otherwise will fall through to next case.

                                                        case "2":
                                                        alert("Thanks for visiting! See you later.");
                                                        break ;

                                                        default :
                                                        alert("Invalid option. Closing game.");
                                                        // No break needed here since this is the end of the switch.
                                                        }

                                                        // I broke the check-you-vs-cpu functionality out into its own function.
                                                        function compareRPS(youC,cpuC) {
                                                        if ( youC === cpuC ) {
                                                        return "It's a tie! Try again to win." ;
                                                        }
                                                        if (choices[youC].win.includes(cpuC)) {
                                                        return "You won! " + youC + " " + choices[youC].action + " " + cpuC + "." ;
                                                        } else {
                                                        return "You lost! " + cpuC + " " + choices[cpuC].action + " " + youC + "." ;
                                                        }
                                                        }


                                                        NOTE: I also switch between const and let. See https://codeburst.io/part-2-var-vs-const-vs-let-69ea73fe76c1 for differences. I mostly use const to indicate a variable I won't change and let to be one that I can (within its proper scope). There's also var, but I didn't need it here.






                                                        share|improve this answer














                                                        I removed some of your variables and combined some, just to make it shorter. I also got rid of the bulk of the if/else since it's not really needed here. For more info on how a switch works, check out https://javascript.info/switch.



                                                        I also changed up your choices so that you can add multiple win or loss conditions for each choice, in case you wanted to upgrade to Rock,Paper,Scissors,Lizard,Spock ( https://www.youtube.com/watch?v=cSLeBKT7-s ).



                                                        // Set up our various choices, how they rank, and their action (can also be array if desired).
                                                        const choices = {
                                                        Rock : { win:["Scissors"] , action:"beats" } ,
                                                        Paper : { win:["Rock"] , action:"beats" } ,
                                                        Scissors : { win:["Paper"] , action:"cuts" } ,
                                                        Spock : { win:["Rock","Scissors"] , action:"beats" }
                                                        } ;

                                                        // Use the keys in choices as our selectable items.
                                                        const weapons = Object.keys(choices) ;

                                                        // Our basic intro.
                                                        const rps = prompt("Welcome to Rock, Paper, Scissors. Would you like to play?" + 'n' + "If you do, enter number 1." + 'n' + "If you don't, enter number 2.");

                                                        // Set the computer choice.
                                                        const chosenOne = weapons[Math.floor(Math.random()*3)];

                                                        // This is an example of your switch.
                                                        switch (rps) {
                                                        case "1" : // Since we used text input, we have to evaluate for a text "number".
                                                        alert("Remember:" + 'n' + " - Rock beats the scissors" + 'n' + " - Paper beats the rock" + 'n' + " - The scissors cut the paper");

                                                        // Make your choice.
                                                        let weapon = prompt("Make your choice:" + 'n' + weapons, "");
                                                        // Is our choice valid?
                                                        if ( !weapons.includes(weapon) ) {
                                                        alert("Invalid choice. Closing Game."); break;
                                                        } else {
                                                        alert("You chose: " + weapon + 'n' + "The computer chose: " + chosenOne);
                                                        }
                                                        // Did I win?
                                                        alert( compareRPS(weapon,chosenOne) ) ;
                                                        break ; // This will break out of the switch. Otherwise will fall through to next case.

                                                        case "2":
                                                        alert("Thanks for visiting! See you later.");
                                                        break ;

                                                        default :
                                                        alert("Invalid option. Closing game.");
                                                        // No break needed here since this is the end of the switch.
                                                        }

                                                        // I broke the check-you-vs-cpu functionality out into its own function.
                                                        function compareRPS(youC,cpuC) {
                                                        if ( youC === cpuC ) {
                                                        return "It's a tie! Try again to win." ;
                                                        }
                                                        if (choices[youC].win.includes(cpuC)) {
                                                        return "You won! " + youC + " " + choices[youC].action + " " + cpuC + "." ;
                                                        } else {
                                                        return "You lost! " + cpuC + " " + choices[cpuC].action + " " + youC + "." ;
                                                        }
                                                        }


                                                        NOTE: I also switch between const and let. See https://codeburst.io/part-2-var-vs-const-vs-let-69ea73fe76c1 for differences. I mostly use const to indicate a variable I won't change and let to be one that I can (within its proper scope). There's also var, but I didn't need it here.







                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited 5 hours ago

























                                                        answered 5 hours ago









                                                        Shawn

                                                        3,3431124




                                                        3,3431124






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.





                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                            Please pay close attention to the following guidance:


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53730900%2fmore-efficient-choice-comparison-for-rock-paper-scissors%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            數位音樂下載

                                                            格利澤436b

                                                            When can things happen in Etherscan, such as the picture below?