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

I think you mean JavaScript and not Java (which is a completely different language).

I am having trouble understanding what you want as a result. You describe storing the function in an array containing the variables of the respective function and their sum. Can you show an example of what that array would look like because in the comment above the functions you wrote is just an array of numbers.

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]

Will all the functions always use all the variables (a, b, and c) inside the functions? I ask because `r1` and `r2` used `x`, `a`, and `b` with the same formula but function `r3` only uses `x` and `a`. Also, how do the numbers in the left column (like the 03.33 and the 01.14) come into play here? I do not see them used in the functions supplied at the top.

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

Is the formula the same for each function or can they be different from each other?

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

Can you upload the excel spreadsheet to Google Sheets and protect the worksheet but make it public?

So the values of x, a, and b are hard coded in each function? If so, that makes it difficult because the only way to get those values returned from the function would be to write the function so that it returned the values along with the calculated value, but if the functions are just as they are, that would not be possible. If however, you can rewrite the functions to only use the values (or not) of x, a, and b, then you could pass those values into the function instead of hard coding and you would know ahead of time what values created the greater value for a given function.

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

There is an npm package that should work with Babel. The online version seems to not be as flexible in the settings as I thought it would be, but I really did not spend much time playing around with the settings.

You can either figure out how to install babel and use this preset that should theoretically work or you can try to learn more about JavaScript and how to manually take what I have given you and try to refactor it into pre ES6 syntax.