Javascript 给定2个正整数值,返回10..20范围内的较大值,如果两者都不在该范围内,则返回0

Javascript 给定2个正整数值,返回10..20范围内的较大值,如果两者都不在该范围内,则返回0,javascript,if-statement,Javascript,If Statement,我的问题是,我似乎无法访问第二个else/if语句,在该语句中,我试图包含一个子句,其中如果一个数字仍然在范围内,它应该仍然返回。语法都是正确的,有人告诉我括号的位置不正确,但我不知道放在哪里,怎么放 var max1020=function(a, b) { if ((a >= 10 && a <= 20) && (b >= 10 && b <= 20)) { if (a > b) { //c

我的问题是,我似乎无法访问第二个else/if语句,在该语句中,我试图包含一个子句,其中如果一个数字仍然在范围内,它应该仍然返回。语法都是正确的,有人告诉我括号的位置不正确,但我不知道放在哪里,怎么放

var max1020=function(a, b) {
    if ((a >= 10 && a <= 20) && (b >= 10 && b <= 20)) { 
        if (a > b) { //comparing my a and my b and returning greater
            return a;  
        } else if (b > a) {
            return b;
        } else if ((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) { 
            if (a >= 10 && a <=20) {
                return a;
            } else if (b >= 10 && b <=20) {
                return b;
            }
        } else {
            return 0;
        }
    }
};
var max1020=函数(a,b){
如果((a>=10&&a=10&&b){//比较我的a和b并返回更大的值
返回a;
}否则,如果(b>a){
返回b;

}else if((a>=10&&a=10&&b=10&&a=10&&b如果a大于b,如果b大于a,则返回。因此,到达第二个else if的唯一时间是a=b?可能我遗漏了什么。

var max1020=函数(a,b){
var max1020=function(a, b) {
 var max = (a>b?a:b);   //first find the max value of a and b 
 if(max>=10&&max<=20) return max; //if max is in range then at 
                                 //least on of them is in range and is bigger
 else if(a>=10 && a<=20) return a; //if the max in not in range then only one could be in range so return the one that in range
 else  if(b>=10 && b<=20) return b;
 return 0;  //if you reach this point none of them was in range
};
var max=(a>b?a:b);//首先找到a和b的最大值 如果(max>=10&&max=10&&a=10&&b您可以使用它,并适当检查是否在范围内

功能范围(a、b){
范围内的函数(v){

return v>=10&&v如果您利用了一些新的ES6功能,您可以使函数更短、更简单、更好

function maxRange(min, max, ...values) {
  values = values.filter(e => e >= min && e <= max);
  return values.length ? Math.max(...values) : 0;
}
这里有一个较长的版本,带有注释,以便更好地理解它是如何工作的

function maxRange(min, max, ...values) {    // Collects the remaining arguments in an array with the rest operator
  values = values.filter(e => {     // Filter the arguments. Using the arrow function
    if(e >= min && e <= max) {      // Test if an argument is in range
      return true;      // Keep it if its in range
    }
    else {
      return false;     // Discard it otherwise
    }
  });
  if(values.length) {   // Test if we have any arguments left
    return Math.max(...values); // Return the highest of them. Using the spread operator
  }
  else {
    return 0;       // Return 0 if no argument was in range
  }
}
函数maxRange(min,max,…value){//使用rest运算符收集数组中的剩余参数
values=values.filter(e=>{//过滤参数。使用arrow函数
如果(e>=最小值和(&e)
function maxRange(min, max, ...values) {    // Collects the remaining arguments in an array with the rest operator
  values = values.filter(e => {     // Filter the arguments. Using the arrow function
    if(e >= min && e <= max) {      // Test if an argument is in range
      return true;      // Keep it if its in range
    }
    else {
      return false;     // Discard it otherwise
    }
  });
  if(values.length) {   // Test if we have any arguments left
    return Math.max(...values); // Return the highest of them. Using the spread operator
  }
  else {
    return 0;       // Return 0 if no argument was in range
  }
}