PDA

View Full Version : Can an array be accepted as a parameter for a function?


Pastafarus
03-13-2008, 06:51 PM
<html><head><title>JavaScript Coding Function</title><script language="javascript" type="text/javascript"><!--Hide this code from very old browsersfunction codeit()//1. will retrieve the text input{test=document.getElementById('gettext').valu e;converttext(test);//2. sends the info to another function called converttext}function converttext(test){for(i=0; i<=test.length; i++) //4. will cut up the word into individual characters.{google=new Array();google[i]=test.substring(i,i+1);<!--//Use the increment operator and substring operator to increment the element index while //incrementing the substring operator to store each single-character element in it's own index HA Ha I figured it out-->function meatandpotatoes(google[i]);}}function meatandpotatoes(google[i]){}//End code hiding from ancient browsers --></script><noscript><center>Your JavaScript cababilities are currently disabled. Please allow Javascript if you wish to use this program.</center></noscript></head><body><center><form name="codeform"><textarea name="textcode" rows="15" cols="15" id="gettext">Hello World</textarea><br/><input type="button" name="button" value="Submit" id="submittext" onClick= 'codeit()'/></center></form></body></html>('gettext').valuethe function meatandpotatoes is supposed to accept the array google as an argument. Firebug keeps on telling me that something is up with my code.

NickJ3682
03-13-2008, 10:08 PM
Not sure about JS, but I know you can do it in C. Too bad you can't use pointers.

HandyManOrNot
03-14-2008, 01:26 AM
You're passing the google array element to the method by name, outside of the method scope where it was declared, and so it is not recognized there. You should create a global variable outside of the individual functions if you want to access the same variable by name like that in multiple functions.Declare it at the top of your Javascript section before the functions (just move the decalration from the method to above the methods. Then all the functions will have access to it.

ArmchairPilot8630
03-14-2008, 04:44 AM
You've declared the function function meatandpotatoes(google[i]);twice, and terminated (second time) it with a semi-colon, making the two end }s invalid. It's not required.HandyManOrNot's point is well taken, but it won't throw an error: put all declarations at the top of the code.Some programming style nitpicks:1) most sources will access the content of a text area usingtest = document.codeform.textcode.value; as indocument.formName.boxName.value2) if you want to convert input into an array of separate characters, another way to do it is google = test.split("");in which case you won't need to declare google as an array, and you can find the number of elements n as n = google.length;