2021年4月24日星期六

Switch statements always executing on the first choice

I'm attempting to create a game of rock, paper, scissors. The problem is my switch statement always executes on the first option. i.e passing a value of (rock, computer) always generates "you win!" (scissors, computer) returns "its a tie" (paper, computer) returns "you lose"

I'm running a set of about 10 ten tests and I notice that the switch statements must be faulty based on my output.

Any ideas? Thanks in advance

  function game(user, computer)  {      if (computer < .34)      {        computer = "rock";      }        else if (computer > .33 && computer < .67)      {        computer = "paper";      }        else       {        computer = "scissors";      }      let result = "";         if (user === "rock")      {        switch(computer)         {          case "scissors":            result = "you win!";            break;            case "paper":            result = "you lose";            break;            case "rock":          result = "its a tie";          break;        }            return result;      }        else if (user === "paper")      {        switch(computer)         {          case "scissors":            result = "you lose!";            break;            case "paper":            result = "its a tie";            break;            case "rock":            result =  "you win!";            break;        }        return result;      }          if (user === "scissors")      {        switch(computer)         {          case "scissors":            result = "its a tie";            break;            case "paper":            result =  "you win!";            break;            case "rock":            result = "you lose!";            break;        }          return result;      }     }```  
https://stackoverflow.com/questions/67249107/switch-statements-always-executing-on-the-first-choice April 25, 2021 at 10:06AM

没有评论:

发表评论