admin管理员组

文章数量:1404593

So I've begun learning HTML/CSS/JavaScript and came across this issue while trying to make an exceedingly simple version of rock-paper-scissors. I believe that the code works fine, but I can't check because it refuses to execute. I have looked extensively for answers on the internet, but can't seem to find any. I have very little experience in JavaScript, I am currently learning it off of Codecademy but I think that resource may be outdated as other websites appear to have conflicting syntax. In short, what am I doing wrong, and which website has it right?

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var puterChoice = Math.random();
                    if (puterChoice < 0.34) {
                        puterChoice = "rock";}
                    else if(puterChoice <= 0.67) {
                        puterChoice = "paper";}
                    else {
                        puterChoice = "scissors";
                    }
                    function pare(choice1,choice2){
                        if (choice1==choice2){
                            pare(userChoice,puterChoice);
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                        }
                        if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                        }
                        if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                            else{
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                        }
                    };
                    pare(userChoice,puterChoice);
                }
                else{
                    document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
                }
            }
            var start = prompt ("Do you want to play?","Yes");}
    </script>
</head>
<body style="text-align:center">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    <button onclick="whole()">Try it out!</button>
    <p id="result">Who won?</p>

</body>
</html>

**Edit: It appears that Codecademy's glossary agrees with other websites, they just haven't gotten around to editing their lessons yet.*

**Edit: Here's my final little code for it. Enjoy its simplicity!*

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play=="Yes"||play=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var puterChoice = Math.random();
                    if (puterChoice < 0.34) {
                        puterChoice = "rock";}
                    else if(puterChoice <= 0.67) {
                        puterChoice = "paper";}
                    else {
                        puterChoice = "scissors";
                    }
                    function pare(choice1,choice2){
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else{
                            alert("Very funny. Now do it right.");
                            game("yes");
                        }
                    };
                    pare(userChoice,puterChoice);
                }
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            var start = prompt ("Do you want to play?","Yes");
            game(start);}
    </script>
    <style>
        body{
            text-align:center;
        }
        #messages{
            font-size:20px;
            color: #00246B;
        }
        #win{
            color: #29A329;
            font-size:18px;
        }
        #loss{
            color:#CC0000;
            font-size:18px;
        }
        a{
            text-decoration:none;
            color:black;
        }
        a:hover{
            font-size:125%;
            color:#B20000;
        }
        button{
            font-size:21px;
        }
    </style>
</head>
<body>
    <a href="">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    </a>
    <button onclick="whole()">Try it out!</button>
    <p id="messages">Who won?</p>
    <p class="result"><span id="loss"></span><span id="win"></span></p>
</body>
</html>

So I've begun learning HTML/CSS/JavaScript and came across this issue while trying to make an exceedingly simple version of rock-paper-scissors. I believe that the code works fine, but I can't check because it refuses to execute. I have looked extensively for answers on the internet, but can't seem to find any. I have very little experience in JavaScript, I am currently learning it off of Codecademy but I think that resource may be outdated as other websites appear to have conflicting syntax. In short, what am I doing wrong, and which website has it right?

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var puterChoice = Math.random();
                    if (puterChoice < 0.34) {
                        puterChoice = "rock";}
                    else if(puterChoice <= 0.67) {
                        puterChoice = "paper";}
                    else {
                        puterChoice = "scissors";
                    }
                    function pare(choice1,choice2){
                        if (choice1==choice2){
                            pare(userChoice,puterChoice);
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                        }
                        if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("result").innerHTML="Paper wins";
                            }
                            else{
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                        }
                        if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("result").innerHTML="Scissors win";
                            }
                            else{
                                document.getElementById("result").innerHTML="Rock wins";
                            }
                        }
                    };
                    pare(userChoice,puterChoice);
                }
                else{
                    document.writeln("<p>Thanks for playing! This was made by Alex</p>";)
                }
            }
            var start = prompt ("Do you want to play?","Yes");}
    </script>
</head>
<body style="text-align:center">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    <button onclick="whole()">Try it out!</button>
    <p id="result">Who won?</p>

</body>
</html>

**Edit: It appears that Codecademy's glossary agrees with other websites, they just haven't gotten around to editing their lessons yet.*

**Edit: Here's my final little code for it. Enjoy its simplicity!*

<html>
<head>
    <title>R,P,S!</title>
    <script type="text/javascript">
        function whole(){
            function game(play){
                if (play=="Yes"||play=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    var puterChoice = Math.random();
                    if (puterChoice < 0.34) {
                        puterChoice = "rock";}
                    else if(puterChoice <= 0.67) {
                        puterChoice = "paper";}
                    else {
                        puterChoice = "scissors";
                    }
                    function pare(choice1,choice2){
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                                document.getElementById("win").innerHTML="";
                            }
                        }
                        else{
                            alert("Very funny. Now do it right.");
                            game("yes");
                        }
                    };
                    pare(userChoice,puterChoice);
                }
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            var start = prompt ("Do you want to play?","Yes");
            game(start);}
    </script>
    <style>
        body{
            text-align:center;
        }
        #messages{
            font-size:20px;
            color: #00246B;
        }
        #win{
            color: #29A329;
            font-size:18px;
        }
        #loss{
            color:#CC0000;
            font-size:18px;
        }
        a{
            text-decoration:none;
            color:black;
        }
        a:hover{
            font-size:125%;
            color:#B20000;
        }
        button{
            font-size:21px;
        }
    </style>
</head>
<body>
    <a href="http://youtu.be/T8r3cWM4JII">
    <h1>JavaScript on a webpage? This is madness!</h1>
    <h2>Madness? THIS... IS... HTML!!!!</h2>
    </a>
    <button onclick="whole()">Try it out!</button>
    <p id="messages">Who won?</p>
    <p class="result"><span id="loss"></span><span id="win"></span></p>
</body>
</html>
Share Improve this question edited Jul 15, 2015 at 18:24 LemenDrop asked Oct 6, 2013 at 22:12 LemenDropLemenDrop 1601 gold badge3 silver badges11 bronze badges 8
  • So what is happening? – Sualkcin Commented Oct 6, 2013 at 22:16
  • The JavaScript isn't executing when I press the button. – LemenDrop Commented Oct 6, 2013 at 22:17
  • 1 You should open up your browser's development console, and check to see if there are any error messages. for example have a look at 'This was made by Alex</p>";)' – Philipp Gayret Commented Oct 6, 2013 at 22:18
  • Your function doesn't do anything except ask if you want to play. – user2736012 Commented Oct 6, 2013 at 22:21
  • 1 Ah. I apparently have an unexpected "}". That should be fun to find. – LemenDrop Commented Oct 6, 2013 at 22:21
 |  Show 3 more ments

2 Answers 2

Reset to default 0

your code is syntatically wrong. put semicolon after paranthesis.

else {
    document.writeln("<p>Thanks for playing! This was made by Alex</p>");
}

Now call the function game inside the closure at the end of function whole.

ie

function() whole(){ 
   function game(play) {
      // your logic
   }

   var start = prompt("Do you want to play?", "Yes");

   game(start);
}

Your game logic is error-prone. recheck your logic.

You are calling the function pare recursively infinitely when choice1 == choice2

just ment the line

if(choice1 == choice2) {
    // pare(userChoice, puterChoice);
}

it will work

You have some issues with your code:

Assignment operator in if statement:

if (play="yes")

This just assigns the string "yes" to the variable play. You should use a parison operator:

if (play === "yes")

Misplaced semi-colon:

document.writeln("<p>Thanks for playing! This was made by Alex</p>";)

Should be:

document.writeln("<p>Thanks for playing! This was made by Alex</p>");

本文标签: JavaScript not executing in my HTMLStack Overflow