繁体   English   中英

添加if-else语句会使JavaScript停止工作

[英]Adding if-else statement causes JavaScript to stop working

我有一个网页设计课程的作业,并且给了我们一些基本的代码,我们需要对其进行修改以做更多的事情。 一旦添加if,else if语句,javascript就会停止工作。 我不知道我要去哪里。 如果有人可以引导我朝正确的方向发展,我将非常感激。 这是原始代码(带有注释,这是我们需要做的事情):

<!DOCTYPE html>
<!-- Finish the logic of the Rock Paper Scissors game below. 
     See the comments for instructions. -->
<html>
    <head>
    <title>RPS</title>
    <script>
    function rps() {
        userChoice = prompt("Rock, paper or scissors?");
        userPick.innerHTML = userChoice;
        computerChoice = Math.random();
        if (computerChoice < 0.34) {
            computerChoice = "rock";
        } else if (computerChoice < 0.67) {
            computerChoice = "paper";
        } else {
            computerChoice = "scissors";
        }
        computerPick.innerHTML = computerChoice;
        result = compare(userChoice, computerChoice);
        if (result == 1) {
            // set the outcome entity to say that "Player Wins!"
            // also increment the user's score.
        } else if (result == -1) {
            // set the outcome entity to say that "Computer Wins!"
            // also increment the computer's score.
        } else {
            // set the outcome entity to say "Tie"
        }
    }

    function compare(choice1, choice2) {
        // return 0 if choices are equal,
        // 1 if choice1 wins,
        // -1 if choice2 wins
    }
    </script>
    </head>
    <body>
        <h2>RockPaperScissors</h2>
        <p id="outcome">-</p>   <b> computer picks: </b>
        <p id="computerPick">-</p>  <b> player picks: </b>
        <p id="userPick">-</p>
        <!-- create p blocks to store the computer's score (number of wins)
        and the user's score. These will have to be modified by the rps function
        -->
        <button type="button" onclick="rps()">Go</button>
    </body>
</html>

这就是我更改函数比较方法的方式:

function compare(choice1, choice2) {
    // return 0 if choices are equal,
    // 1 if choice1 wins,
    // -1 if choice2 wins

    if (choice1 == "paper" && choice2 == "rock" || 
        choice1 == "rock" && choice2 == "scissors" || 
        choice1 == "scissors" && choice2 == "paper") {
        return == 1;
    }
    else if (choice1 == "paper" && choice2 == "scissors" || 
             choice1 == "rock" && choice2 == "paper" || 
             choice1 == "scissors" && choice2 == "rock") {
        return == -1;
    }
    else {
        return == 0;
    }
}

从您的退货中删除==。 他们应该像...

返回1;

==用于比较。

将您的compare功能更改为

function compare(choice1, choice2) {
            // return 0 if choices are equal,
            // 1 if choice1 wins,
            // -1 if choice2 wins

            if (choice1=="paper" && choice2=="rock"||choice1=="rock"
&& choice2=="scissors"||choice1=="scissors" && choice2=="paper"){
                return 1;}
            else if (choice1=="paper" &&
choice2=="scissors"||choice1=="rock" && choice2=="paper"||choice1=="scissors" &&
choice2=="rock"){
                return -1;}
            else {
                return 0;}
        }
           return 1

           return == 1

Return是关键字而不是变量。 =是赋值运算符,它将运算符右侧的值放入左侧的变量中。 ==是相等运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM