Findd the greater function

Hi ,
New here and new to java…
I’m trying to get the largest number of this 3 functions then store this function
in an array containing the variables of the respective function and their sum
…is this possible ?
Thank you.

//this must be the like this
// var result = [50,55,60,165]  //  function r2

function r1(){
 var x = 5;
 var a = 10;
 var b = 10;
return x+a+b // 25
}
function r2(){
   var x = 50;
   var a = 55;
   var b = 60;
return x+a+b // 165
}
function r3(){
   var x = 25;
   var a = 15;
   var b = 0
return x+a // 40
}

thanks for reply
I have an excel calculator that I would like to translate into javascript.
the simple idea is to obtain 4 variables comparing this functions with the largest number
in the attached example function 2 is the greater,so the result I would like to be this new variables or array:

result
var a = 3
var b = 1
var c = 5
var t = 8

or

[3,1,5,8]

the left column is rounded to the right column, so the left column does not matter.
at the r3 function i added another var .now all functions have 3 variables
607-13-2022

// var result = [3,1,5,9]  //  function r2

function r1(){
 var x = 2;
 var a = 2;
 var b = 1;
return x+a+b // 5
}
function r2(){
   var x = 3;
   var a = 1;
   var b = 5;
return x+a+b // 9
}
function r3(){
   var x = 3;
   var a = 2;
   var b = 0
return x+a //6
}

they are not the same, the numbers may be different, but there will always be 3 variables
I can’t attaach the xls file, it would have been much better understood

So after thinking about it a bit more, if the value of x, a, and b are hardcoded in each of the functions, then the only thing that could be returned is either the greatest value like below:

function r1(){
 var x = 2;
 var a = 2;
 var b = 1;
return x+a+b // 5
}
function r2(){
   var x = 3;
   var a = 1;
   var b = 5;
return x+a+b // 9
}
function r3(){
   var x = 3;
   var a = 2;
   var b = 0
return x+a //5
}

function findGreater(...functions) {
  return functions.reduce((calc, func) => {
    const result = func();
    return calc === null ? result : Math.max(result, calc); 
  }, null);
}

console.log(findGreater(r1, r2, r3)); // 9

Or if you also want to know the function name that produces the greater value, then you could return an object like:

function r1(){
 var x = 2;
 var a = 2;
 var b = 1;
return x+a+b // 5
}
function r2(){
   var x = 3;
   var a = 1;
   var b = 5;
return x+a+b // 9
}
function r3(){
   var x = 3;
   var a = 2;
   var b = 0
return x+a //5
}

function findGreater(...functions) {
  return functions.reduce((resultObj, func) => {
    const greater = func();
    const functionName = func.name;
    return resultObj === null
      ? { greater, functionName }
      : greater > resultObj.greater
        ? { greater, functionName }
        : resultObj;
  }, null);
}  
console.log(findGreater(r1, r2, r3)); // { greater: 9, functionName: 'r2' }

One way to return the greater value along with corresponding values of x, a, and b would be to pass them along with the function but the functions would need to just return the calculated results of the values of x, a, and b passed to them. The result of the function is an array containing the greater value, the values of x, a, and b (in order), and also the function name that calculated the greater value.

function r1(x, a, b){
  return x+a+b
}
function r2(x, a, b){
  return x+a+b
}
function r3(x, a, b){
  return x+a
}

function findGreater(...functions) {
  const greaterResult =  functions.reduce((resultObj, [ func, ...args ]) => {
    const greater = func(...args);
    const functionName = func.name;
    return resultObj === null
      ? { greater, args, functionName }
      : greater > resultObj.greater
        ? { greater, args, functionName }
        : resultObj;
  }, null);
  const { greater, args, functionName } = greaterResult;
  return [ greater, ...args, functionName ];
}  
console.log(findGreater([r1,2,2,1], [r2,3,1,5], [r3, 3, 2, 0])); // [ 9, 3, 1, 5, 'r2' ]

Thank you very much for your help.
I tested the script below in Visual Studio and it’s exactly what I need.
The only problem is that for the script I want to create I use ExtendScript Toolkit, which does not know ES6 language.
I tried to transform with Babel but it doesn’t work

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.