混淆Javascript语法错误
我正在测试三个通过的数字中哪一个是最大的,并通过一个函数返回。然而,我不断得到一个语法错误:SyntaxError:missing;before语句}else(第三个>第一个&第三个>第二个){ 代码如下:混淆Javascript语法错误,javascript,Javascript,我正在测试三个通过的数字中哪一个是最大的,并通过一个函数返回。然而,我不断得到一个语法错误:SyntaxError:missing;before语句}else(第三个>第一个&第三个>第二个){ 代码如下: function getMax3(first, second, third) { if (first > second && first > third){ return first; } else if (second > first &&am
function getMax3(first, second, third) {
if (first > second && first > third){
return first;
} else if (second > first && second > third) {
return second;
}else (third > first && third > second) {
return third;
}
}
console.log(getMax3(10, 3, 4));
console.log(getMax3(1, 6, 9));
正如其他人所指出的,您当然可以使用来解决这个特定的问题 您不能在
else
上设置条件。您可以将最后一个else
更改为else if
,也可以简单地删除该条件,让else捕获第一个条件未捕获的所有内容
因此,要么:
function getMax3(first, second, third) {
if (first > second && first > third){
return first;
} else if (second > first && second > third) {
return second;
}else {
return third;
}
}
console.log(getMax3(10, 3, 4));
console.log(getMax3(1, 6, 9));
或
试试这个
function getMax3(first, second, third) {
if (first > second && first > third){
return first;
} else if (second > first && second > third) {
return second;
}else {//the else cannot have condition
return third;
}
}
console.log(getMax3(10, 3, 4));
console.log(getMax3(1, 6, 9));
这是因为从您的编码中,我们可以看到您已将条件包含在else语句中。有关更多信息,请查看>>
这是修改后的代码。
试试这个……您可以将所有元素放入数组中,并添加此原型,以便在所需的任意多个元素中找到最大值:
Array.prototype.max = function () {
return Math.max.apply(null, this);
};
例:
你可以简单地使用
Math.max(10,3,4)
。那么…这是什么else(third>first&&third>second)
请看一看:否则永远不会有条件。
function getMax3(first, second, third) {
if(first > second && first > third){
return first;
} else if(second > first && second > third) {
return second;
}else if(third > first && third > second) {
return third;
}
}
console.log(getMax3(10, 3, 4));
console.log(getMax3(1, 6, 9));
Array.prototype.max = function () {
return Math.max.apply(null, this);
};
var arr = [4,5,6,7,73,53,123,53];
var maxVal = arr.max();