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?
hbar1st
December 27, 2022, 3:31pm
2
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?
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
@camperextraordinaire thanks, all clear now
system
Closed
June 29, 2023, 9:22am
9
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.