Proless
Proless

Reputation: 217

min and max from a variable of type number in JS

I was studying min and max methods in JavaScript and I couldnt find any post that explain these methods algorithms. My guess is that in numerical variable like 1234 you can get min and max by splitting the number in 4 values and compare them. Or add these values in array, sort that array and get min as array[0]. Is there any post related to these methods explained?

Upvotes: 2

Views: 286

Answers (2)

rojo
rojo

Reputation: 24476

In the current source of the v8 JavaScript Engine, /build/v8/src/math.js defines Math.min() and Math.max() thusly:

Math.min:

// ECMA 262 - 15.8.2.12
function MathMin(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(arg2);
    if (arg2 > arg1) return arg1;
    if (arg1 > arg2) return arg2;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg1 : arg2;
    }
    // All comparisons failed, one of the arguments must be NaN.
    return NAN;
  }
  var r = INFINITY;
  for (var i = 0; i < length; i++) {
    var n = %_Arguments(i);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
    // Make sure -0 is considered less than +0.
    if (NUMBER_IS_NAN(n) || n < r || (r === 0 && n === 0 && %_IsMinusZero(n))) {
      r = n;
    }
  }
  return r;
}

Math.max:

// ECMA 262 - 15.8.2.11
function MathMax(arg1, arg2) {  // length == 2
  var length = %_ArgumentsLength();
  if (length == 2) {
    arg1 = TO_NUMBER_INLINE(arg1);
    arg2 = TO_NUMBER_INLINE(arg2);
    if (arg2 > arg1) return arg2;
    if (arg1 > arg2) return arg1;
    if (arg1 == arg2) {
      // Make sure -0 is considered less than +0.
      return (arg1 === 0 && %_IsMinusZero(arg1)) ? arg2 : arg1;
    }
    // All comparisons failed, one of the arguments must be NaN.
    return NAN;
  }
  var r = -INFINITY;
  for (var i = 0; i < length; i++) {
    var n = %_Arguments(i);
    if (!IS_NUMBER(n)) n = NonNumberToNumber(n);
    // Make sure +0 is considered greater than -0.
    if (NUMBER_IS_NAN(n) || n > r || (r === 0 && n === 0 && %_IsMinusZero(r))) {
      r = n;
    }
  }
  return r;
}

If you use Function.prototype.apply() with Math.min() or Math.max(), you can get the minimum or maximum value for a sequence of numbers.

(borrowing from adeneo's comment above because I'm lazy:)

Math.max.apply(Math, '1234'.split(''));
// returns 4

Upvotes: 1

Eugene Naydenov
Eugene Naydenov

Reputation: 7295

Math.min and Math.max are returning you the minimum and the maximum value respectively from the set of passed arguments.
So that you can just split a string representation of your number to retrieve an array of digits and then apply Math.min or Math.max methods to the result array.

(function() {
  var minDigit,
      maxDigit,
      numToDigitsArray;
  
  numToDigitsArray = function(num) {
    return ('' + Math.abs(num)).split('').map(function(digit) {
      return parseInt(digit);
    });
  };
  
  minDigit = function(num) {
    return Math.min.apply(null, numToDigitsArray(num));
  };
  
  maxDigit = function(num) {
    return Math.max.apply(null, numToDigitsArray(num));
  };
  
  console.log(minDigit(-5343240123));
  console.log(maxDigit(753943240123));
})();

Upvotes: 1

Related Questions