0

Web デザイン クラスの課題があり、さらに多くのことを行うために変更する必要がある基本的なコードが与えられました。if、else if ステートメントを追加するとすぐに、javascript が機能しなくなります。どこが間違っているのかわかりません。誰かが私を正しい方向に導くことができれば、本当に感謝しています. 元のコードは次のとおりです (必要なことを 1 つメモしてあります)。

<!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;
    }
}
4

3 に答える 3

1

返品から == を削除します。彼らはただのようでなければなりません...

1 を返します。

== は比較に使用されます。

于 2013-03-08T03:05:24.437 に答える
0

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;}
        }
于 2013-03-08T03:05:29.017 に答える
-1
           return 1

いいえ

           return == 1

Returnは変数ではなくキーワードです。=は代入演算子で、演算子の右側の値を左側の変数に入れます。そして==は等式演算子です。

于 2013-03-08T03:03:51.597 に答える