LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2010, 02:09 PM   #1
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Rep: Reputation: 62
Trying to understand this function in Javascript


Hi,
In my book, this is an example of a recursive algorithm:

var reverseArray = function(x,indx,str) {
return indx == 0 ? str : reverseArray(x,--indx,(str+= " " + x[indx])) ;;
}

var arr = new Array('apple','orange','peach','lime');
var str = reverseArray(arr,arr.length,"");
alert(str) ;



I'm trying to understand it, I'm stuck at the (str+= " " + x[indx])) part - that part of the function definition expects an ARGUMENT, am I right? What's being passed to it is a bit of PROGRAM, the str+= stuff. So - how is it working?
 
Old 12-09-2010, 02:37 PM   #2
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
The str+= " " + x[indx] part is a bit of shorthand. What it is doing is appending " " + x[indx] to str and then passing str as an argument to reverseArray(). It is identical to writing:

Code:
--indx;
str += " " + x[indx];
reverseArray(x,indx,str);
 
Old 12-09-2010, 02:45 PM   #3
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
But how is this happening? How will I know what forms a part of an argument?
 
Old 12-09-2010, 02:55 PM   #4
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
The commas between the parentheses delineate the arguments to a function.
 
Old 12-09-2010, 02:59 PM   #5
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
No no, I know that, I meant how will I know that it's taking str as the argument from (str+= " " + x[indx]) ?
 
Old 12-09-2010, 03:05 PM   #6
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
The entire expression evaluates to the value of str after the right hand side of the assignment is done.
 
Old 12-09-2010, 03:10 PM   #7
resetreset
Senior Member
 
Registered: Mar 2008
Location: Cyberspace
Distribution: Dynebolic, Ubuntu 10.10
Posts: 1,340

Original Poster
Rep: Reputation: 62
Sh*t, I didn't know this. Thanks a lot for your help, tsg
 
Old 12-09-2010, 03:51 PM   #8
tsg
Member
 
Registered: Mar 2008
Posts: 155

Rep: Reputation: 30
You're welcome.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
JavaScript function disappears after getting called for once mohtasham1983 Programming 2 02-23-2021 11:15 PM
javascript - class / function in function jchambers Programming 3 06-15-2009 10:33 PM
Calling a function in javascript mohtasham1983 Programming 4 03-04-2008 04:13 PM
How to understand chdir function? xeon123 Programming 1 10-15-2007 05:53 PM
help with javascript function paul_mat Programming 3 02-15-2006 12:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration