JavaScript Rock, Paper, Scissors game with confirm and prompt not working as expected – how to fix?

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

JavaScript Rock, Paper, Scissors game with confirm and prompt not working as expected - how to fix?

问题

我正在学习JavaScript,以创建一个带有确认和提示的“剪刀石头布”游戏。

当我运行代码时,有两个问题。

  1. 在提示框上,如果我点击确定但没有输入r、s或p,它不会显示“你没有选择r、p、s”。
  2. 游戏在运行,但有时需要输入两次才能显示结果。

以下是您提供的代码的翻译部分:

let playgame = confirm("您想玩游戏吗?");
if (playgame) {
  let startgame = prompt("从 r、p、s 中选择一个?");
  if (startgame) {
    let playerchoice = startgame.trim().toLowerCase();
    if (playerchoice === "r" || playerchoice === "s" || playerchoice === "p") {
      let compchoice = Math.floor(Math.random() * 3 + 1);
      let computer = compchoice;
      if (compchoice === 1) {
        computer = "r";
      }
      if (compchoice === 2) {
        computer = "s";
      }
      if (compchoice === 3) {
        computer = "p";
      }

      if (playerchoice === computer) {
        alert("平局");
      }
      if (playerchoice === "r" && computer === "s") {
        alert("电脑赢了");
      }
      if (playerchoice === "p" && computer === "r") {
        alert("玩家赢了");
      }
      if (playerchoice === "s" && computer === "p") {
        alert("玩家赢了");
      }
    } else {
      alert("您没有选择r、p、s");
    }
  } else {
    alert("改变主意了吗?没问题");
  }
} else {
  alert("明白了,也许下次吧");
}
英文:

I am learning JavaScript to create a "Rock, Paper, Scissors.” game with confirm and prompt.

I have two issues when I run the code.

  1. On prompt, when i click ok without input r,s,p, it doesn't show "you didn't choose r,p,s"
  2. The game is working, however, sometimes i will need to enter twice to make the result come out.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let playgame = confirm(&quot;Do you want to play game?&quot;);
if (playgame) {
  let startgame = prompt(&quot;Choose from r, p, s?&quot;);
  if (startgame) {
    let playerchoice = startgame.trim().toLowerCase();
    if (playerchoice === &quot;r&quot; || playerchoice === &quot;s&quot; || playerchoice === &quot;p&quot;) {
      let compchoice = Math.floor(Math.random() * 3 + 1);
      let computer = compchoice;
      if (compchoice === 1) {
        computer = &quot;r&quot;;
      }
      if (compchoice === 2) {
        computer = &quot;s&quot;;
      }
      if (compchoice === 3) {
        computer = &quot;p&quot;;
      }


      if (playerchoice === computer) {
        alert(&quot;tie game&quot;);
      }
      if (playerchoice === &quot;r&quot; &amp;&amp; computer === &quot;s&quot;) {
        alert(&quot;computer win&quot;);
      }
      if (playerchoice === &quot;p&quot; &amp;&amp; computer === &quot;r&quot;) {
        alert(&quot;player win&quot;);
      }
      if (playerchoice === &quot;s&quot; &amp;&amp; computer === &quot;p&quot;) {
        alert(&quot;player win&quot;);
      }


    } else {
      alert(&quot;you didn&#39;t choose r, p, s&quot;);
    }
  } else {
    alert(&quot;change you mind? not a problem&quot;);
  }
} else {
  alert(&quot;Got you, Maybe next time&quot;);
}

<!-- end snippet -->

答案1

得分: 1

  1. 如果您在提示对话框上点击取消,startgame变量将为null。如果您什么都不输入,只是点击确定,startgame变量将为空字符串""。在这两种情况下,条件如下:
if (startgame)

将始终为假。因此,您可以添加条件如if (startgame !== null)

  1. 实际上,在这个代码块中,您没有涵盖所有情况。
if (playerchoice === computer) {
  alert("平局");
}
if (playerchoice === "r" && computer === "s") {
  alert("电脑赢了");
}
if (playerchoice === "p" && computer === "r") {
  alert("玩家赢了");
}
if (playerchoice === "s" && computer === "p") {
  alert("玩家赢了");
}

例如,当电脑是"r"而玩家选择"s"时,等等。

英文:
  1. if you click on cancel on prompt dialog startgame variable will be null. If you do not type anything and just click on Ok, startgame variable will be "" (an empty string). In both cases, condition such as:

> if (startgame)

Will be always falsy. So you can add condition like if (startgame !== null)

  1. Actuaclly you did not cover all the cases in this block.

> if (playerchoice === computer) {
> alert("tie game");
> }
> if (playerchoice === "r" && computer === "s") {
> alert("computer win");
> }
> if (playerchoice === "p" && computer === "r") {
> alert("player win");
> }
> if (playerchoice === "s" && computer === "p") {
> alert("player win");
> }

For example when computer is "r" and playerchoice is "s", etc.

huangapple
  • 本文由 发表于 2023年5月28日 08:03:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349479.html
匿名

发表评论

匿名网友

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

确定