[JavaScript] Maximum value

Write a method to find the largest of the two numbers a and b. Do not use if-else or other comparison operators.

Example:

Input: a = 1, b = 2
Output: 2
The first reaction to this question is definitely a relatively large one, but no matter what the question requires, only other mathematical methods can be considered.

Code:

/**
 * @param {number} a
 * @param {number} b
 * @return {number}
 */
var maximum = function(a, b) {
    
    
   var s=(a+b)/2;/*取得两数的平均数s*/
   s=s+Math.abs(a-s);/*s值加上到其中任何一个数的距离*/
   return s;
};

Guess you like

Origin blog.csdn.net/weixin_42345596/article/details/105013271