r.fleisch
r.fleisch

Reputation: 11

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range

My problem is that I can't seem to access the second else/if statement where I try to include a clause where if a number is still in range it should still return. The syntax is all correct, I was told my brackets are improperly placed but I can't fathom where or how.

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;
        }
    }
};

Upvotes: 1

Views: 316

Answers (4)

Kruga
Kruga

Reputation: 791

If you take advantage of some of the new ES6 features, you can make your function shorter, simpler, and better.

function maxRange(min, max, ...values) {
  values = values.filter(e => e >= min && e <= max);
  return values.length ? Math.max(...values) : 0;
}

You can specify the range and you are not limited to only 2 values. You can use it like this

maxRange(10, 20, 10, 15)  // Returns 15
maxRange(10, 20, 10, 15, 20, 30)  // Returns 20
maxRange(10, 20, 5, 25, 99)  // Returns 0
maxRange(100, 150, 99, 120, 200)  // Returns 120
maxRange(-50, -20, 12, -77, 123, -24) // Returns -24

Here is a bit longer version with comments to better understand how it works.

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
  }
}

Here's some more info on the features I've used

Rest Operator

Spread Operator

Arrow Function

Array Filter

Conditional (Ternary) Operator

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could use Math.max for it with a proper check if in range.

function range(a, b) {
    function inRange(v) {
        return v >= 10 && v <= 20 ? v : 0;
    }
    return Math.max(inRange(a), inRange(b));
}

console.log(range(-1,-5));
console.log(range(1, 5));
console.log(range(10, 5));
console.log(range(10, 15));
console.log(range(100, 15));

Upvotes: 1

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34160

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
};

Upvotes: 0

SaschaThullner
SaschaThullner

Reputation: 56

It seems that you're returning if a is bigger than b and if b is bigger than a. So the only time you would reach the second else if is when a = b? Maybe I missed something.

Upvotes: 0

Related Questions