Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript get max sum of multiple functions

NewHere

New Coder
I'm trying to get the largest number of this 3 functions then store in an array, the variables of that function and their amount..can be done?

JavaScript:
//this must be the result
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;
return x+a // 40
}
 
Variables defined inside of a function can not be retrieved outside of the function. The function must pass them to the outside. So the return statement of each func has to be rewritten and 'b' must be declared in func r3

Code:
<script>
function r1(){
    var x = 5;
    var a = 10;
    var b = 10;
    let arry = [x, a, b, x+a+b];
    return arry;
}
function r2(){
    var x = 50;
    var a = 55;
    var b = 60;
    let arry = [x, a, b, x+a+b];
    return arry;
}
function r3(){
    var x = 25;
    var a = 15;
    var b = 0;
    let arry = [x, a, b, x+a+b];
    return arry;
}

/* These are the arrays of each function */
let arry1 = r1();
let arry2 = r2();
let arry3 = r3();

/* This array is the highest number of the last 3 arrays */
let maxArry = [arry1[3], arry2[3], arry3[3]];

/* This returns the max number in an array of numbers */
let most = maxArry.reduce((a, b) => { return Math.max(a, b) });

/* Proof of the largest number */
alert(most);
</script>
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom