Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取javascript函数中返回的正确值时出现问题_Javascript_Function_Return - Fatal编程技术网

获取javascript函数中返回的正确值时出现问题

获取javascript函数中返回的正确值时出现问题,javascript,function,return,Javascript,Function,Return,我要做的是在一个页面中模拟一个简单的纸、石头、剪刀游戏,其中一个玩家用单选按钮选择r/p/s,提交他的选择,玩家2也会这样做 我毫不怀疑这段代码存在多个问题,但每当我尝试运行解决石头/布/剪刀问题的函数时,我都会看到返回的非常奇怪的东西。我总是得到一个真实的答案: else if (player1Choice("Rock") && player2Choice("Scissors")) { $("resultOutput").value = "Player 1s R

我要做的是在一个页面中模拟一个简单的纸、石头、剪刀游戏,其中一个玩家用单选按钮选择r/p/s,提交他的选择,玩家2也会这样做

我毫不怀疑这段代码存在多个问题,但每当我尝试运行解决石头/布/剪刀问题的函数时,我都会看到返回的非常奇怪的东西。我总是得到一个真实的答案:

else if (player1Choice("Rock") && player2Choice("Scissors")) {
        $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors";
    }
我每次都把这个作为结果拿回来。我盯着这个函数看了这么久,现在可能对一个明显的解决方案一无所知

// create our $() shortcut function for easily retrieving elements by id
var $ = function(id) {
    return document.getElementById(id);
}

//function executed on page load
window.onload = function() {

    //clear any previous values
    document.forms[0].reset();

    //store value from player1Weapon radio choice with the player1Choice function
    $("player1Submit").onclick = player1Choice;

    //store value from player1Weapon radio choice with the player2Choice function
    $("player2Submit").onclick = player2Choice;

    //assign the fight button to run the fightCrunch function to determine the winner
    $("fight").onclick = fightCrunch;
}

var player1Choice = function(x){
    var a = "Paper";
    var b = "Rock";
    var c = "Scissors";
   //the html has several radio buttons with id's of "player1Paper", etc. the $ function is to get the id name, and then I'm doing if, else ifs to see which on is checked.
    if ($("player1Paper").checked) {
        return a;
}
else if ($("player1Rock").checked) {
    return b;
}
else if ($("player1Scissors").checked) {
    return c;
}
else {
    alert("Player 1, you need to pick a crude instrument of violence first");
}
};

var player2Choice = function(y){
    var d = "Paper";
    var e = "Rock";
    var f = "Scissors";
    //var d = $("player2Paper").value;
    //var e = $("player2Rock").value;
    //var f = $("player2Scissors").value;
    if ($("player2Paper").checked) {
        return d;
}
else if ($("player2Rock").checked) {
    return e;
}
else if ($("player2Scissors").checked) {
    return f;
}
else {
    alert("Player 2, you need to pick a crude instrument of violence first");
}
};

var fightCrunch = function(){
        //The next few lines are commented out because the if gets hung up on this part, this always comes back as true no matter what I do with the previous functions.
    //if (player1Choice || player2Choice == undefined) {
        //alert("Both players need to select and submit a weapon of choice before a winner is determined");
    //}
    if (player1Choice && player2Choice == "Rock") {
        $("resultOutput").value = "Both players chose Rock, which results in a stalemate";
    }
    else if (player1Choice && player2Choice == "Paper") {
        $("resultOutput").value = "Both players chose Paper, which results in a stalemate";
    }
    else if (player1Choice && player2Choice == "Scissors") {
        $("resultOutput").value = "Both players chose Scissors, which results in a stalemate";
    }
    else if (player1Choice("Rock") && player2Choice("Scissors")) {
        $("resultOutput").value = "Player 1s Rock beats Player 2s Scissors";
    }
    else if (player1Choice("Rock") && player2Choice("Paper")) {
        $("resultOutput").value = "Player 2s Paper beats Player 1s Rock";
    }
    else if (player1Choice("Paper") && player2Choice("Rock")) {
        $("resultOutput").value = "Player 1s Paper beats Player 2s Rock";
    }
    else if (player1Choice("Paper") && player2Choice("Scissors")) {
        $("resultOutput").value = "Player 2s Scissors beats Player 1s Paper";
    }
    else if (player2Choice("Paper").value && player1Choice("Scissors")) {
        $("resultOutput").value = "Player 1s Scissors beats Player 2s Paper";
    }
    else if (player2Choice("Rock").value && player1Choice("Scissors")) {
        $("resultOutput").value = "Player 2s Rock beats Player 1s Scissors";
    }
    else {
        alert("something is wrong here");
    }
}

你的代码有很多问题。但你所问的问题源于你做比较的方式

您不能这样做:

if (player1Choice && player2Choice == "Rock")
这意味着:

if ((player1Choice == true) && (player2Choice == "Rock"))
相反,您希望以这种方式编写它(但由于许多其他错误,它仍然无法工作):

不仅比较操作更少,还节省了许多代码行

请注意,在过去的两次比较中,您错误地添加了“.value”,因此您还有其他打字错误

除此之外,您可能希望注意函数
player1Choice
player2Choice
不是变量。您已将它们指定为单击事件的事件处理程序。它们返回的值不适用于任何地方,并且不会被
fightcrunch
函数接收

我真的不想破坏你制作这个程序的乐趣,但是如果你真的放弃了,你可以在这里看到正确的和功能性的代码(如果你不想看到它,它在右边有一个标签):


这里有一大堆问题。最大的问题似乎是:

  • 假设
    player1Choice
    是一个字符串值(例如
    player1Choice==“Rock”
    )和假设它是一个函数(
    player1Choice(“纸张”)
    )。即使假设它是一个函数,也会假设它返回一个布尔真/假值(
    if(player1Choice(“Paper”))
    ),而函数实际返回的是一个字符串值(
    “Paper”
    )。因此,每次您检查
    player1Choice
    player1Choice(“纸张”)
    ,它都将评估为
    true
    ,这显然是行不通的

在a中创建它,我会让它为您工作。第一次使用JSFIDLE,现在就检查它。张贴。提前感谢您的输入!他没有使用jQuery。他将$定义为document.getElementByIdOK的别名,因此我尝试使用click事件处理程序生成存储所选单选按钮值的函数。最简单的方法是什么,让某些东西在onclick上运行,然后返回按钮或标签值,然后通过fightCrunch函数访问?我所做的大部分工作是练习函数和返回语句。我知道它看起来很复杂(是的,你的代码运行得很好,谢谢!),但这里额外的是为了满足一些任务要求。再次感谢您的投入!我懂了。在这种情况下,您需要全局变量,并将它们存储在某个位置。我会再做一次编辑。嘿,这些东西帮了我大忙。非常感谢!
if (player1Choice == player2Choice) {
    $("resultOutput").value = "Both players chose " + player1Choice + ", which results in a stalemate";
}
                                                                                <form>
                                                                                    Player 1
                                                                                    <select id="player1">
                                                                                        <option value="0" selected>Paper</option>
                                                                                        <option value="1">Rock</option>
                                                                                        <option value="2">Scissors</option>
                                                                                    </select>

                                                                                    Player 2
                                                                                    <select id="player2">
                                                                                        <option value="0" selected>Paper</option>
                                                                                        <option value="1">Rock</option>
                                                                                        <option value="2">Scissors</option>
                                                                                    </select>

                                                                                    <input type="submit" id="fight" value="Fight">
                                                                                </form>

                                                                                <div id="resultOutput"></div>


                                                                                <script> 
                                                                                    // create our $() shortcut function for easily retrieving elements by id
                                                                                    var $ = function(id) {
                                                                                        return document.getElementById(id);
                                                                                    }

                                                                                    //function executed on page load
                                                                                    window.onload = function() {

                                                                                        //clear any previous values
                                                                                        document.forms[0].reset();

                                                                                        $("fight").onclick = fightCrunch;
                                                                                    }

                                                                                    var fightCrunch = function(){

                                                                                        var choices = ["Paper", "Rock", "Scissors"];
                                                                                        var player1 = $("player1").value;
                                                                                        var player2 = $("player2").value;
                                                                                        var result = "";
                                                                                        var diff = player1 - player2;

                                                                                        if (!diff)
                                                                                            result = "Both players chose " + choices[player1] + ", which results in a stalemate";
                                                                                        else if (diff == -1 || diff == 2)
                                                                                            result = "Player 1's " + choices[player1] + " beats Player 2's " + choices[player2];
                                                                                        else
                                                                                            result = "Player 2's " + choices[player2] + " beats Player 1's " + choices[player1];

                                                                                        $("resultOutput").innerHTML = result;
                                                                                        return false;
                                                                                    }

                                                                                </script>
var player1Choice, player2Choice;

window.onload = function () {

    //clear any previous values
    document.forms[0].reset();

    //store value from player1Weapon radio choice with the getPlayer1Choice function
    $("player1Submit").onclick = function () {
        player1Choice = getPlayer1Choice();
    };

    //store value from player1Weapon radio choice with the getPlayer2Choice function
    $("player2Submit").onclick = function () {
        player2Choice = getPlayer2Choice();
    };

    //assign the fight button to run the fightCrunch function to determine the winner
    $("fight").onclick = fightCrunch;
}

function getPlayer1Choice () {
    //...
    // return ...;
}

function getPlayer2Choice () {
    //...
    // return ...;
}