Quiz, challenge, js, why?, | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Quiz, challenge, js, why?,

Can someone help and tell me why the output of the code below is 7 ? ---------------------------------------------------------- function abc(a){ return(function(y) { return y+1; }) (++a)+a; } alert(abc(2));

18th Feb 2018, 1:17 PM
Amadeus
Amadeus - avatar
4 Answers
+ 6
I just didn't know why the returned function is executed...
18th Feb 2018, 1:29 PM
Amadeus
Amadeus - avatar
+ 2
++2 gives 3, so value of a is now 3. 3+3+1 = 7
18th Feb 2018, 1:23 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 1
You execute your function in return statement. (function(y){ y+1; })(++a) // ++a is an argument of your function You execute it with ++a. Preincrementation works before going into function hence "a" is 3. Then function adds 3+1 which is 4 and then abc function returns result of inner function and adds a, so it gives 4 + 3.
18th Feb 2018, 5:52 PM
Jakub Stasiak
Jakub Stasiak - avatar
0
As Jakub said above 😄 you can review this lesson if you want.. https://www.sololearn.com/learn/JavaScript/1130/ Good luck my friend 💚
18th Feb 2018, 1:27 PM
Baraa AB
Baraa AB - avatar