-1

I'm learning javascript and I found this exercise:

function abc(a){
    return(function(y){
    return y--**--y+1;
  })(++a)+a;
}
document.write(abc(2));

The output of that is 7 and I dont understand why, I searched information about nested functions and I didnt found anything....

How is "y" defined?

vatz88
  • 2,422
  • 2
  • 14
  • 25
  • y is 3 - does that help? – Jaromanda X Feb 25 '17 at 05:37
  • or that you are doing `(Math.pow((2 + 1), ((2 + 1) - 1) - 1) + 1) + (2 + 1)` – Jaromanda X Feb 25 '17 at 05:41
  • `function(y){...}(++a)` is an immediately invoked function expression ... y == ++a or, 3 in this case – Jaromanda X Feb 25 '17 at 05:43
  • This is less about the nested function and more of the strange math that's going on. Looks like it's using the new `**` operator. – Joseph Feb 25 '17 at 05:43
  • the question is "how is y defined" - I think IIFE explains that – Jaromanda X Feb 25 '17 at 05:44
  • While this may be educational, this kind of code should never appear in the wild. Using the `++` and `--` operators like that is [bad practice](http://stackoverflow.com/q/971312/5743988). – 4castle Feb 25 '17 at 05:52
  • Wherever you are learning JavaScript, I'd advise finding somewhere else that explains things before tossing you complicated code like this - ideally only a single concept should be explained per example at least until you have all of the parts down. – Gerrit0 Feb 25 '17 at 05:56
  • Forget about what`s happening inside the return y--**--y+1; You can add from Right to Left just the (++a)+a this is the only part that gets printed: (1+1+2)1+2 = (1+1+2)3 = (1+1+5) = (1+6) = (7). – Juniar Feb 25 '17 at 06:12

3 Answers3

2

this function translates to:

//I've replaced the arguments and the vars with the actual values
function abc(){
    function innerFunction(){
        return (3 ** 1) + 1
    }
    return innerFunction(3) + 3
}

//or written differently
function abc(a){
    function innerFunction(v){
        //v--
        var tmp1 = v;
        v = v-1;

        //--v
        v = v-1;
        var tmp2 = v;

        return Math.pow(tmp1, tmp2) + 1;
    }

    a = a+1; //++a
    return innerFunction(a) + a;
}
Thomas
  • 11,958
  • 1
  • 14
  • 23
0

That is easy. Function abc return (function(y){ return y--**--y+1; })(++a)+a this means if a is 2 (function(3){ return y--**--y+1; }) + 2

0

This contains an immediately invoked function expression (IIFE).

Here's an example that might clarify how they work:

(function(x) {  return x;  })(10);  // Returns 10

The value inside the parenthesis is used as the function's argument as soon as the function is defined. So, in this case, the function is set to return the argument, and immediately afterwards, it is given the value 10.

On your example, with some added whitespace:

return (function(y) {
           return y-- ** --y + 1;
       })(++a)
+ a

What's happening is the same. A function is being defined then, immediately afterwards, it receives the argument ++a. It will replace all instances of y by the value contained in ++a. Then, as that is returned, it goes through + a, so it adds a to the result.

Matheus Avellar
  • 1,507
  • 1
  • 22
  • 29