Javascript Return and if/else
Javascript Return and if/else
对于以下代码,我不断为alert
取回Scissors
。我不知道我在这里做错了什么。
var computerChoice = Math.random();
var newChoice = function (computerChoice) {
if (computerChoice <= 0.34) {
var newChoice = "rock";
return newChoice;
} else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
var newChoice = "paper";
return newChoice;
} else {
var newChoice = "scissors";
return newChoice;
}
}
var newerChoice = newChoice();
alert(newerChoice);
调用参数时没有将其传递给newChoice
,因此参数computerChoice
的值为undefined
。undefined <= 0.34
是false
,其他比较相同。
两种可能的解决方案是:
- 从函数中删除参数,以便
computerChoice
引用全局变量。当前,参数对具有相同名称的外部变量进行阴影处理 - (更好)用参数调用函数
了解有关函数的更多信息。
变量和参数之间似乎有点混淆。这里有一些工作代码。。。
var newChoice = function (choice) {
if (choice <= 0.34) {
return "rock";
} else if (choice <= 0.66) {
return "paper";
} else {
return "scissors";
}
};
var computerChoice = Math.random();
var newerChoice = newChoice(computerChoice);
alert(newerChoice);
传递到newChoice
中的值是参数choice
。我删掉了&&
,因为它们不是必需的。如果选择不是<=0.34,那么它必须大于0.34,这样以后就不需要检查了。
computerChoice
是作为参数传递给函数的随机值变量。
这是一个工作小提琴的例子。。。
http://jsfiddle.net/ArchersFiddle/0dfhoa63/
newChoice方法的computerChoice
参数是undefined
,因为调用newChoice()
中没有传递任何内容。如果你想传递之前生成的号码,你可以按照以下方式进行:
var computerChoice = Math.random();
var newChoice = function(computerChoice) {
if (computerChoice <= 0.34) {
return "rock";
} else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
return "paper";
} else {
return "scissors";
}
}
var newerChoice = newChoice(computerChoice);
alert(newerChoice);
尽管将computerChoice
声明为参数,但实际上并没有将其作为变量传递给函数。用var newerChoice = newChoice(computerChoice);
代替var newerChoice = newChoice();
。我认为函数将查找computerChoice作为参数,而不是查看前面的声明。在javascript中查找变量范围。
newChoice
函数需要一个参数。所以你必须给它一个
var newerChoice = newChoice(computerChoice);
此外,我相信在第一个if语句中,您希望有newChoice = "rock";
,而不是computerChoice = "rock";
var newerChoice = newChoice(Math.random());
仔细看一下这一行:
var newChoice = function (computerChoice) {
您创建了一个名为newChoice
的函数,并且需要一个称为computerChoice
的参数。现在让我们看看稍后如何执行该函数
var newerChoice = newChoice();
您执行了函数newChoice
,但这次没有将computerChoice
作为参数传递。
解决方案
你能做的事相当容易。更改以下内容:
var newChoice = function (computerChoice) {
进入
var newChoice = function () {
通过将computerChoice = Math.random();
添加到函数中,使newChoice
函数为computerChoice
生成一个新值。像这样:
var computerChoice = Math.random();
var newChoice = function () {
computerChoice = Math.random();
if (computerChoice <= 0.34) {
var computerChoice = "rock";
return newChoice;
} else if ((computerChoice >= 0.35) && (computerChoice <= 0.66)) {
var newChoice = "paper";
return newChoice;
} else {
var newChoice = "scissors";
return newChoice;
}
}
var newerChoice = newChoice();
alert(newerChoice);
- Jquery else if statement
- Javascript skips else if?
- JqueryUI,多个ID和else if 语句在可丢弃,我如何让它们工作
- JQuery Else if statement?
- 以这种方式使用if/else-if/else是一种很好的做法
- switch(true)作为else-if的替代
- else-if子句未按预期工作
- 使用if-else-if-true检查(验证)单选按钮如果未检查则无警报必须检查警报
- Javascript else-if语句错误
- `else if `从不执行;只执行前面的“if”
- 正在创建嵌套的if+else-if语句,但函数返回为未定义
- else-if条件在js渲染模板中
- 我的if和else-if逻辑中的问题以及两个函数的冲突
- 带有if语句的循环的Javascript未到达else-if语句
- 使用 If else if else 语句得到错误
- if-else-if JavaScript 返回 true 并退出,无论输入如何
- Jquery else if is not working
- 如何在 Angular JS 中执行开关或 else/if 语句
- 我可以在 html 文档中使用 if/else if 命令吗?
- 我需要帮助“调试”If/Else if/Else 语句的语法