More efficient choice comparison for Rock Paper Scissors

前端 未结 6 875
你的背包
你的背包 2021-01-07 23:54

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 al

6条回答
  •  北海茫月
    2021-01-08 00:19

    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;
    

提交回复
热议问题