How to put the response of a function as a parameter in another function?

Asked

Viewed 106 times

2

const soma = function (a,b) {return a+b}

console.log(soma(2,4))

let nun1 = 3

const soma2 = function (soma, nun1) {return soma+nun1}

I want to get the result of the sum function which is 6 and somar with nun1 which is 3

How can I take this result and put in the second function?

  • 1

    It would be something like let nun1 = 3 + soma(2,4); ?

  • If that question isn’t marked as duplicate, I don’t know what else it could be. (not that I want it to be marked, but I think it’s politics and what’s been happening, right ?) And it’s not even the fault of the boy who asked, because he’s probably new here.

  • 2

    @Thiaguinhoembasamento if you know that it is duplicate signaling indicating which it is, otherwise do not speculations without foundation that do not help at all.

2 answers

1


You’re calling soma() and taking the result to print on the console, not holding anywhere, so you can’t use this value unless you call the function again. As the function is pure would have no problem except the loss of performance by doing twice the same thing. But this code does not even make sense.

Then doesn’t call soma2() nowhere.

To put the result of a function as a parameter of another function is to do exactly what you did on the console, is passing the result of soma() as a function parameter console.log(), only you did with the wrong function, you want to do it in the soma2().

Then just call the function passing this, and send print to see.

I have my doubts whether this code is teaching something or creating confusion.

I gave one organized in the code.

const soma = function(a, b) { return a + b; }
let nun1 = 3;
const soma2 = function(soma, nun1) { return soma + nun1 }
console.log(soma2(soma(2, 4), nun1));

I put in the Github for future reference.

1

Welcome to Sopt, what you want seems to be a function that makes a sum from another sum... That is, just take the result of the first sum and add with the result of another sum:

let nun1 = 3

const soma = function(a, b) {
    return a + b;
}

soma(nun1, soma(1, 4));

You can do this several times, example:

soma(1, soma(2, soma(3, soma(4, soma(5 + 1)))))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.