为什么不提示工作?

时间:2013-12-18 00:28:07

标签: javascript prompt

我正在通过Codecademy尝试学习Javascript。当我到达纸/岩石/剪刀部分时,我想出了一些代码来展示我的朋友。这不是一场真正的比赛。我觉得这很有趣。无论如何,我在Codecademy中写了它并且它有效。然后我将它复制/粘贴到textedit(我正在使用mac / safari),我添加的只是响应的标题标签。但现在提示不起作用。帮助一个菜鸟。

<!DOCTYPE html>
<html>
<body>
<h1>
Rock. Paper. Scissors.
</h1>
<script>

var yourChoice = prompt("Choose your weapons: Rock, paper, or scissors.")

if(yourChoice === "rock")
document.write("<h1 style='text-align:center;font-size:50;'>Ha! I defeated you with the cunning of paper!</h1>")

if(yourChoice === "paper")
document.write("<h1 style='text-align:center;font-size:50;'>Muahaha! I have cut you down with the villanny of scissors!"</h1>)

if(yourChoice === "scissors")
document.write("<h1 style='text-align:center;font-size:50;'>Go and weep! For I have crushed your devilish scissors with the might of my rock!"</h1>)

</script>
</body>
</html>

希望这不是一件非常明显的事情,因为某些原因我只是视而不见。先谢谢你们。

4 个答案:

答案 0 :(得分:2)

您错误地将结束"放错了两次:

if(yourChoice === "paper")
    document.write("<h1 style='text-align:center;font-size:50;'>Muahaha! I have cut you down with the villanny of scissors!</h1>")

if(yourChoice === "scissors")
    document.write("<h1 style='text-align:center;font-size:50;'>Go and weep! For I have crushed your devilish scissors with the might of my rock!</h1>")

Try it

答案 1 :(得分:0)

试试这个:

<body onload="onLoad()"> 
   <script>

    function onLoad() {
      var yourChoice = prompt("Choose your weapons: Rock, paper, or scissors.")

      if(yourChoice === "rock")
    document.write("<h1 style='text-align:center;font-size:50;'>Ha! I defeated you with the cunning of paper!</h1>")

      if(yourChoice === "paper")
    document.write("<h1 style='text-align:center;font-size:50;'>Muahaha! I have cut you down with the villanny of scissors!</h1>")

      if(yourChoice === "scissors")
    document.write("<h1 style='text-align:center;font-size:50;'>Go and weep! For I have crushed your devilish scissors with the might of my rock!</h1>")
    }
    </script>

答案 2 :(得分:0)

只需在右引号内包含第二个标签(对于第二个和第三个if语句)。

使用Chrome开发者工具来帮助您发现JavaScript错误。 https://developers.google.com/chrome-developer-tools/

答案 3 :(得分:0)

您将结束"置于错误的位置......

if(yourChoice === "paper")
document.write("<h1 style='text-align:center;font-size:50;'>Muahaha! I have cut you down with the villanny of scissors!</h1>")

if(yourChoice === "scissors")
document.write("<h1 style='text-align:center;font-size:50;'>Go and weep! For I have crushed your devilish scissors with the might of my rock!</h1>")

使用语法高亮...这是发现这一点的最简单方法。您还应该学习在浏览器中查看javascript控制台以查找错误。这特别是扔了一个:

  

Uncaught SyntaxError:无效的正则表达式:missing /

虽然这不是确切的错误,但它通常会提供关于可能存在的位置的提示。