Pergunta

I'm going through Codecademy trying to learn Javascript. When I got to the paper/rock/scissors part, I came up with some code to show my friend. It's not really a game. I made it to be funny. Anyway, I wrote it in Codecademy and it worked. Then I copy/pasted it to textedit (I'm using mac/safari) and all I added were the header tags for the responses. But now the prompt doesn't work. Help a noob.

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

Hopefully it's not something super obvious that I'm just blind to for some reason. Thank you guys, in advance.

Foi útil?

Solução

You misplaced the closing " twice:

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

Outras dicas

try this:

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

Just include the second tags inside the closing quotation mark (for the second and third if statements).

Use Chrome Developer Tools to help you spot javascript errors. https://developers.google.com/chrome-developer-tools/

You put the closing " in the wrong spot...

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>")

Use syntax highlighting... That's the easiest way to spot this. You should also learn to look at your javascript console in your browser for errors. This in particular threw a:

Uncaught SyntaxError: Invalid regular expression: missing /

Although it's not the exact error, it often provides hints as to where that might lie.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top