How do you add multiple arguments from different functions into one function containing all the parameters?

Viewed 42

I have a question and not looking for a solution to the code printed below, the example is just there for reference.

I would like to know if it's possible to add multiple arguments from different functions into one function containing all the parameters.

Basically, I would like to know how to write this code correctly.

I LIKE TO STATE I AM NEW TO JAVASCRIPT AND MAY NOT KNOW HOW TO WRITE THESE QUESTIONS OUT CORRECTLY ON OVER-STACK FOR YOU PROFESSIONAL TO ANSWER.

This is a fake example code

Step 1: I have a main function that holds all the parameters, x, y, z.


function sumNum(x, y, z) {

return (x * y) + z;

}

Step 2: Question - What do I need to add within the brackets as an argument to link the X parameter in the above function in step 1.


function num1() {

sumNum() 

}

Step 3: Question - What do I need to add within the brackets as an argument to link the Y parameter in the above function in step 1.


function num2() {

sumNum()

}

Step 4: Question - What do I need to add within the brackets as an argument to link the Z parameter in the above function in step 1.


function num3() {

sumNum()

}

I understand that you need to call the arguments in the order you have them set as parameters, but I want to know if there are other ways of writing this code to get the same results.

2 Answers

You can do something like this for example you have your 3 functions, each of them receive 1 parameter

function num1(param) {
  //your code for num1, return a value
  return param
}


function num2(param) {
  //your code for num2, return a value
  return param
}


function num3(param) {
  //your code for num3, return a value
  return param
}

Then you can call them all inside single function that will receive all 3 parameters needed for num1, num2 and num3

function sumNum(x, y, z) {
    var value1 = num1(x);
    var value2 = num1(y);
    var value3 = num1(z);
    return (value1 * value2) + value3;
}

Then call your function with the needed parameters

var result = sumNum(1,2,3)
console.log(result);//your output = 5

Without using JQuery and using the code I got from user "Chris G", I believe this is the best answer to my question. Please leave comments if I have written this wrong so I can correct it.


<button id= "btn">Get Answer</button>


var numVal1 = 2;
var numVal2 = 6;
var numVal3 = 9;

document.getElementById("btn").addEventListener("click", function() {
funcPara(numVal1, numVal2, numVal3);
})

function funcPara(x, y, z) {

num1(x);
num2(y);
num3(z);

}


function num1(para) {

console.log(`num1 = ${para}`);  

}


function num2(para) {

console.log(`num2 = ${para}`);

}


function num3(para) {

console.log(`num3 = ${para}`);

}
Related