Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > passing parameter of a function to array

Reply
Thread Tools

passing parameter of a function to array

 
 
cuco
Guest
Posts: n/a
 
      02-22-2011
I want to use the parameter of a function as a variable for an array.

Example:
<html>
<head>
my function (parameter){
x=new Array(parameter);
// rest of the code using x
}
</head>
<body>
<script language="javascript">my function(" ' item1' , ' item2' , '
item3' ")</script>
</body>
</html>

It's not working.
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-22-2011
cuco wrote:

> I want to use the parameter of a function as a variable for an array.
>
> Example:
> <html>
> <head>
> my function (parameter){
> x=new Array(parameter);
> // rest of the code using x
> }
> </head>
> <body>
> <script language="javascript">my function(" ' item1' , ' item2' , '
> item3' ")</script>
> </body>
> </html>


Your code is strewn with syntax errors.

> It's not working.


<http://jibbering.com/faq/notes/posting/#ps1DontWork>
<http://validator.w3.org/>
<http://getfirebug.com/>


PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      02-22-2011
On 2/22/2011 4:28 PM, cuco wrote:
> I want to use the parameter of a function as a variable for an array.
>
> Example:
> <html>
> <head>
> my function (parameter){
> x=new Array(parameter);
> // rest of the code using x
> }
> </head>
> <body>
> <script language="javascript">my function(" ' item1' , ' item2' , '
> item3' ")</script>
> </body>
> </html>
>
> It's not working.


Oh my, you make a load of mistakes.

1) "my function" is not a legal function name.
2) You forgot to place javascript tags around the part in the header.
3) Stop using language="javascript". Use: type="text/javascript" instead.
4) Use a ; to end a statement.
5) Do not try to pass around the array definition. Pass around a
reference to the array.
6) Get in the habbit of using var. You are poluting the global namespace
without it.

A lot more can be said, but I stop here.


So, for starters, try this:

<html>
<head>
<script type="text/javascript">
myFunction (anArray){
var x=anArray;
// The above is probably nonsense because you could use
// anArray too, instead of x.
// rest of the code using x
}
</script>

</head>
<body>

<script type="text/javascript">
var myArray = ['item1' , 'item2' , 'item3'];
myFunction(myArray);
</script>
</body>
</html>


Not tested.
Erwin Moller


--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      02-22-2011
On Feb 22, 6:28*pm, cuco <(E-Mail Removed)> wrote:
> I want to use the parameter of a function as a variable for an array.


As Pointed out, my function() {... is either an error or a typo for
JavaScript

For the rest your actual aim is not clear. If you need a function that
takes any amount of arguments and the exact amount may change at
runtime then use automatically created for you "arguments" list:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8">
<script>
function myFunction() {
for (var i=0; i<arguments.length; i++) {
window.alert(arguments[i]);
}
}
</script>
</head>
<body>
<p>
<button type="button" onclick="
myFunction('a', 'b');
">Call with 2 args</button>
</p>
<p>
<button type="button" onclick="
myFunction('a', 'b', 'c');
">Call with 3 args</button>
</p>
</body>
</html>

If you need a function that - as in your example - gets a single
string containing comma-delimited values, parses that string and
stores each found value in an array then it is a whole different
question.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Passing Two-Dimensional Array as a Function Parameter Kai-Uwe Bux C++ 2 10-03-2010 02:09 PM
Passing parameter to function not expecting parameter Mister B C Programming 8 08-26-2010 08:01 AM
How to pass a parameter for a function parameter in a function AzamSharp Javascript 2 07-05-2008 12:24 AM
creating multidimensional array at runtime and passing it as parameter to a function nitinm C Programming 2 09-14-2007 09:30 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments