Quiz, challenge, js, why?, | Sololearn: Learn to code for FREE!

+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));

2/18/2018 1:17:20 PM

Amadeus

4 Answers

New Answer

+6

I just didn't know why the returned function is executed...

+2

++2 gives 3, so value of a is now 3. 3+3+1 = 7

+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.

0

As Jakub said above 😄 you can review this lesson if you want.. https://www.sololearn.com/learn/JavaScript/1130/ Good luck my friend 💚