How to calculate the output of invoking 2 functions in 1 expression in JavaScript?

How to calculate the output of invoking 2 functions in 1 expression in JavaScript? Represented in this case by
`square(plus1(y))

let x = 2, y = 3;

function plus1(x) {
return x + 1;
}

plus1(y)

let square = function(x) {
return x * x;
};

square(plus1(y))`

The value of square(plus1(y)) should be 16

But square(y) is 9
and plus1(y) is 4
so the sum of these functions is 13 not 16

How to get the correct result that is 16?

Your code gives me 16 when I test it.
Can you clarify the question?

square(plus1(y))

let x = 2, y = 3;

function plus1(x) {
return x + 1;
}

plus1(y)

let square = function(x) {
return x * x;
};

square(plus1(y))

I agree, the code is correct, maybe you didn’t save it before running it, or another
code editor problem.

@hbar1st @goodCamelCase thanks for your answers, I tested it out in vs code too and the result is 16, why is 16 and not 13?

setting let x = 2, y = 3;

But square(y) is 9
and plus1(y) is 4

square(plus1(y)) is the result of the sum of square(y) + plus1(y) ?

or I think probably square(plus1(y) = square(x) * plus1(y) = 2 ² * 4 = 16 , right?

The above squares the result of plus1(y), so if y is equal to 3, then plus1(3) returns 4. The square of 4 is 16. If you are getting 13, please post all of the code.

What you wrote on the right side of the = is not what is happening with square(plus1(y)). The value of x does not come into play here. The plus1(3) is evaluated first and passed to the square function, so it is really just sqare(4) which returns 16.

1 Like

@RandellDawson thanks, all clear now :sweat_smile: