1

Im reading John Resigs "Learning Advanced JavaScript" http://ejohn.org/apps/learn/#10 and came across this function below that I don`t understand.

The function yell is called with an argument of 4. When this is done, the argument 4 is run through the terniary operator. If it is greater than zero, which it is, then we come to yell(n-1) + a My questions relate to this.

a) does yell(n-1) + a call the function again (i.e. restart it) with an argument of 3.

b) If you do (n-1) + a, you get a result of 3a. Does JavaScript convert 3a to "aaa". I ask this because in the assert line it says yell(4) == "hiyaaaa"

c) after converting it, does it somehow add it to "hiy"? I don`t understand how.

d) if JavaScript does convert 3a to a string of "aaa"s, and somehow manages to add it to "hiy", I don`t understand why yell(4)=hiyaaaa. Yell(n-1) + a = hiyaaa (3as), not hiyaaaa(4"a"s).

As you can see I am totally confused.

function yell(n){
  return n > 0 ? yell(n-1) + "a" : "hiy";
}
assert( yell(4) == "hiyaaaa", "Calling the function by itself comes naturally." );
1
  • For a), it is known as a recursive function, and yes it is calling self with 3. Commented Mar 17, 2011 at 0:45

4 Answers 4

1

a) This function is taking advantage of recursion, so yes, the function called again and everything else is pushed on the stack waiting for that return value. b) No, the function is called with a return value as mentioned above. c) See Above. d) It doesn't.

Think of it like this:

function a(val) {
  return val + "a";
}

function b(val) {
  return a(val) + "a";
}

If you call b("hiya") you'll get hiyaaa. Now instead of calling a different function, call the same one.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you. When you say, everything is pushed on the stack waiting for the return value, do you mean when n=0 and the function returns "hiy" If so, was it the "a"s that were pushed onto the stack? and if so, how do they get added to the end of "hiy" to make "hiyaaaa" and therefore yell(4) = hiyaaaa? Please explain in as much detail as you can.
Basically the entire function call is pushed onto the stack. You can think of the return value of the ternary operator as "append 'a' to the value this function returns." So when n is greater than zero it says it is waiting for a return value so it can append 'a' to that value, which ultimately is going to be "hiy". Since it had 4 as the original number, there are 3 a's waiting to be appended.
1

a) Yes it calls the function again, so the function is recursive.

b) If one of the operands is a string, + does string concatenation.

c) It returns it.

d) Write the recursion out on paper to better visualise it.

5 Comments

thank you. regarding (c), I dont understand how "hiy" ever gets returned. Provided that n > 0, the terniary chooses yell(n-1) + a, and ignores "hiy". Once the function gets called 4 times, n=0, and only then the final part of the terniary gets chosen. So it returns "hiy", but when thats the case, how do the "a"s get added to it to make yell(4)=hiyaaa?
I'd look at my example below for this to make a little more sense.
@mjmitche The ternary operator says when less or equal to 0, return "hiy".
thanks for clarifying and @myles I still don`t understand how, when "hiy" is returned it adds the 4"a"s. Were they stored on the stack and why are they put onto the end of "hiy" there is no concatenation in the function that puts them on the end of "hiy".
@mjmitche I believe the final call to yell() will return it and add one more a.
1

For a), it is known as a recursive function, and yes it is calling self with 3.

For b) you aren't just doing (n-1), you are doing yell(n-1) + "a", and once n =0, yell(0) returns 0.

For c), read the last part of b), i.e. because of the ternary statement, it will return "hiy" once n=0.

For d), see the rest of them.

1 Comment

thank you very much. I understand it will return "hiy" once n=0, but I don`t understand why that means yell(4)=hiyaaaa. During the first 4 passes through the function when it does yell(n-1) + a, does it somehow store the "a"s that it is adding, and then, once n=0, somehow add them to the "hiy" that gets returned? If so, how? Please explain in as much detail as possible as I am a bit of a newbie.
0

Try replacing the "a" with the argument supplied to the yell function. Like this:

function yell(n){  return n > 0 ? yell(n-1) + n : "hiy"; }
var x = yell(4); log(x);
assert( yell(4) == "hiy1234", "Calling the function by itself comes naturally." );

So, each value of n is taken and put on a LIFO stack ie 4,3,2,1 and when n becomes 0 "hiy" is on top of the LIFO stack. Then the concatenation is done be popping each value off of the stack such that the string becomes "hiy1234".

Hope this helps.

Bumpfster

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.