+ 2

var r = (function (x, f = () => x) { var x; var y=x; x = 2; return (x + y + f()); })(1) console.log(r);

Can you explain why r = 4

9th Dec 2017, 8:52 AM
Luke
Luke - avatar
4 ответов
+ 4
oh so that f() is counted as 1 . Sorry that I guessed that wrong .
9th Dec 2017, 10:07 AM
RZK 022
RZK 022 - avatar
+ 2
first you passed a parameter to the function which has value = 1. You have stored it in x x = 1 then you stored it in y y = x as x = 1 so y = 1 then you changed the value of x to 2 x = 2 in return a value you returned x + y + f() as f contains no parameter , so it will be zero hence it will return x + y = 1 + 2 = 3
9th Dec 2017, 8:58 AM
RZK 022
RZK 022 - avatar
+ 2
first you passed a parameter to the function which has value = 1. You have stored it in x x = 1 then you stored it in y y = x as x = 1 so y = 1 then you changed the value of x to 2 x = 2 in return a value you returned x + y + f() as f contains no parameter , so it will be zero hence it will return x + y = 1 + 2 = 3 and f()=1 then x + y + f() = 1+2+1 = 4 example f()=2 It will be 2 + 2 +2 = 6 f() = 3 3 + 2 + 3 = 8
9th Dec 2017, 9:49 AM
Luke
Luke - avatar
- 1
but result is r = 4
9th Dec 2017, 9:01 AM
Luke
Luke - avatar