JavaScript – 为什么这个 switch case 语句不起作用?

huangapple go评论63阅读模式
英文:

Java Script- Why doesn't this switch case statement work?

问题

Here is the translated code portion:

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;
}
};
英文:

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 JavaScript – 为什么这个 switch case 语句不起作用?

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;
}
};

答案1

得分: 2

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!`;
英文:

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!`;

答案2

得分: 1

switch 只支持一个值。要将其用作开关,您需要将字符串连接起来。

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

另一种选择是嵌套开关。

switch (p1){
  case "scissors":
    switch (p2){
      case "paper":
        return "Player 1 won!";
      case "rock":
        return "Player 2 won!";
      case "scissors":
        return "Draw!";
  case "paper":
    ....
英文:

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":
    ....

答案3

得分: 0

这不是 switch 的工作方式。Switch 寻找给定语句的精确值匹配。

你想要使用 if,就像这样:

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

  // 更多的 if 语句...
}

或者,如果你坚持要使用 switch,可以比较组合的 string 值,就像这样:

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

    // ...更多的情况...
  }
}

此外,请注意,你不需要在返回之后使用 break。返回语句会立即退出你的函数。

英文:

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.

答案4

得分: 0

以下是您要翻译的内容:

开关检查单个条件。 但是,您可以交换检查并在每种情况下测试真实性,给出两个参数的检查。

注意,除非您想在同一范围内重新分配“rps”,否则应使用普通函数或const而不是let。 此外,return 表示每个return 后不需要break。 而且您没有默认返回(噢!)。 您还可以将结果分组,因为穿越结果都会返回并停止匹配情况。

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" and 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!";
};
英文:

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!"
};

huangapple
  • 本文由 发表于 2023年4月6日 19:45:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定