skip to Main Content

I’m new to JS and I’m trying to do an assignment on codewars. It’s supposed to be a rock,paper, scissors game and to return which player won or if there was a draw. I tried using switch case for this solution, however in some cases it returns the wrong answer. I know it can be solved using the if statement, however I am curious why my solution does not work. Could someone explain? Thank You in advance 🙂

Here is what I tried:

let rps = (p1, p2) => {
  switch (p1,p2){
  case "scissors" && "paper":
    return "Player 1 won!";
      break;
  case "scissors"&& "rock":
    return "Player 2 won!";
       break;
  case "scissors"&& "scissors":
    return "Draw!";
       break;
  case "paper"&& "paper":
    return "Draw!";
       break;
  case "paper"&& "scissors":
    return "Player 2 won!";
       break;
  case "paper"&& "rock":
    return "Player 1 won!";
       break;
  case "rock"&& "rock":
    return "Draw";
       break;
  case "rock"&& "paper":
    return "Player 2 won!";
       break;
  case "rock" && "scissors":
    return "Player 1 won!";
       break;
}
};

4

Answers


  1. switch only supports one value. To use it as a switch you would need to concatenate the strings.

    switch (p1 + p2){
      case "scissorspaper":
        return "Player 1 won!";
      case "scissorsrock":
        return "Player 2 won!";
      case "scissorsscissors":
        return "Draw!";
    

    other option is a nested switch

    switch (p1){
      case "scissors":
        switch (p2){
          case "paper":
            return "Player 1 won!";
          case "rock":
            return "Player 2 won!";
          case "scissors":
            return "Draw!";
      case "paper":
        ....
    
    Login or Signup to reply.
  2. That is not how switch works. Switch looks for exact value match to the given statement.

    You want to use either if like so:

    const response = (p1, p2) => {
      if (p1 === "scissors" && p2 === "paper") {
        return "Player 1 won!";
      }
    
      // more ifs...
    }
    

    Or, if you insist of using switch, compare for example string value of the combination, like so:

    const response = (p1, p2) => {
      const combination = `${p1}:${p2}`;
      switch (combination) {
        case "scissors:paper":
          return "Player 1 won!";
    
        // ...more cases...
      }
    }
    

    Also, note that you don’t need to break after return. Return statements exits your function immediately.

    Login or Signup to reply.
  3. A switch statement only takes one expression. Note that p1,p2 applies the comma operator, which will result in the value of p2. Your case expressions only check one expression. Also here the && operator will evaluate to one of both operands.

    There are a few things you can do to reduce the code repetition here:

    • The three words have distinct first letters, so you could make the logic only look at first letters
    • If you place those distinct letters in a string, then you can make it that if two letters are consecutive, that means the first player won.

    So, you can do this:

    const rps = (p1, p2) => {
      if (p1 === p2) return "Draw!";
      if ("sprs".includes(p1[0] + p2[0])) return "Player 1 won!";
      return "Player 2 won!";
    }
    

    Using the same logic, but putting it in one expression:

    const rps = (p1, p2) =>
       p1 === p2 ? "Draw!" : `Player ${2-"sprs".includes(p1[0] + p2[0])} won!`;
    
    Login or Signup to reply.
  4. A switch checks a single condition. You can swap the check, however, and test for true given a check of the two arguments in each case.

    Note, you should also use a plain function or const instead of let, unless you want to reassign rps later in the same scope. Also, the return means you don’t need a break after each return. And you don’t have a default return (doh!). You can also group the outcomes, since the fall through outcomes all return and stop the case matching.

    const rps = (p1, p2) => {
      switch (true) {
        case p1 === "scissors" && p2 === "scissors":
        case p1 === "paper" && p2 === "paper":
        case p1 === "rock" && p2 === "rock":
          return "Draw!";
        case p1 === "scissors" && p2 === "paper":
        case p1 === "paper" && p2 === "rock":
        case p1 === "rock" && p2 === "scissors":
          return "Player 1 won!";
        case p1 === "scissors" && p2 === "rock":
        case p1 === "paper" && p2 === "scissors":
        case p1 === "rock" && p2 === "paper":
          return "Player 2 won!";
      }
    
      return "Doh!"
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search