【问题标题】:I cant seem to return the result of inside the function我似乎无法返回函数内部的结果
【发布时间】:2017-12-29 21:35:45
【问题描述】:

我创建了一个基本的石头剪刀布游戏,我无法在函数内返回结果。

我什么都试过了

链接控制台记录比较并将其放入变量中..

console.log(compare) 由于某种原因返回 undefined。

请帮忙

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
	computerChoice = "rock";
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
} else {
	computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 
  

var compare = function(userChoice, computerChoice){
    if (userChoice === computerChoice){
        return    "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }
     
}

【问题讨论】:

  • return = "The result is a tie!";?为什么是等号?
  • 哦,这是一个诚实的错字,但删除它后,我仍然遇到同样的问题
  • 好吧,我测试了 sn-p,它工作正常。我认为你相信它不会返回任何东西,因为你没有对返回的字符串做任何事情
  • 你可以比较一个函数然后alert(compare(userChoice,computerChoice))
  • 这里解释了差异:stackoverflow.com/questions/4314434/…

标签: javascript function


【解决方案1】:

这里的主要问题是您从未执行过您编写的匿名函数。我比较了一个普通函数而不是匿名函数,只是为了使它与其他语言更相似。这是一个很好的工作代码:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 


function compare(userChoice, computerChoice){
    if (userChoice === computerChoice){
        return    "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }

}
console.log(compare(userChoice, computerChoice));

【讨论】:

    【解决方案2】:

    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random();
    var result;
    
    if (computerChoice < 0.34) {
    	computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    	computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } console.log("Computer's choice: " + computerChoice);
      console.log("Your choice: " + userChoice);
      console.log(computerChoice + " vs " + userChoice); 
    
    function compare(userChoice, computerChoice){
        if (userChoice === computerChoice){
            return    "The result is a tie!"; 
        } else if (userChoice ==="rock"){
           if(computerChoice ==="scissors"){
            return "rock wins!";    
           }
           else {
             return "paper wins!";       
           }
        } else if (userChoice === "paper") {
            if(computerChoice === "rock") {
            return "paper wins!"; 
            }
            else {
            return "scissors wins!";
            }
        } else if (userChoice === "scissors") {
            if(computerChoice === "rock") {
            return "rock wins!"; 
            }
            else {
            return "scissors wins!";
            }
        }
         
    }
    var Compare = compare(userChoice, computerChoice);
    console.log(Compare);

    【讨论】:

    • 但是如果你想要和你一样的代码,你可以使用类似 var compare = (function(comp,user){ ... })()
    【解决方案3】:

    正如 Shinratensei 所说,您在第 18 行有一个 = 导致错误。 这是一个更新的截图。我还为比较功能添加了警报。

    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random();
    var result;
    
    if (computerChoice < 0.34) {
    	computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    	computerChoice = "paper";
    } else {
    	computerChoice = "scissors";
    } console.log("Computer's choice: " + computerChoice);
      console.log("Your choice: " + userChoice);
      console.log(computerChoice + " vs " + userChoice); 
      
      alert(compare(userChoice,computerChoice));
    
     function compare (userChoice, computerChoice){
        if (userChoice === computerChoice){
            return  "The result is a tie!"; 
        } else if (userChoice ==="rock"){
           if(computerChoice ==="scissors"){
            return "rock wins!";    
           }
           else {
             return "paper wins!";       
           }
        } else if (userChoice === "paper") {
            if(computerChoice === "rock") {
            return "paper wins!"; 
            }
            else {
            return "scissors wins!";
            }
        } else if (userChoice === "scissors") {
            if(computerChoice === "rock") {
            return "rock wins!"; 
            }
            else {
            return "scissors wins!";
            }
        }
         
    }

    【讨论】:

      【解决方案4】:

      这里有语法错误...

      return  =  "The result is a tie!"; 
      

      将其更改为:

      return "The result is a tie!"; 
      

      【讨论】:

      • 这是一个诚实的错字。但实际上我仍然有同样的问题!也许是因为我无法控制台记录正确的值?
      • @RedondoVelasco 您需要在代码底部调用compare();以显示返回语句。否则你可以console.log()字符串。
      猜你喜欢
      • 2014-09-04
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2011-12-21
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多