I am trying to pass arrays of various lengths to a function.
However the values are read as a string.
What is the easiest way to pass the values?
<script type="text/javascript">
<!--
function foo() {
var args = "start ";
for( var i=0 ; i < arguments.length; i++ ) {
if(i==0)
args += arguments[i];
else if(i%2) // odd
args += ' (' + arguments[i];
else
args += ', ' + arguments[i] + ')';
}args += ' end';
return args;
}
myArray1 = new Array('x', 2, 3)
myArray2 = new Array('y', 5, 7, 11, 13)
alert(foo(myArray1)); // start 1,2,3 end
alert(foo(myArray2)); // start 2,3,5,12,13 end
// alerts should be: start 1 (2, 3) end
// start 2 (3, 5) (12, 13) end
// -->
</script> 2 29578 al*******@excite.com (aliensite) writes: I am trying to pass arrays of various lengths to a function. However the values are read as a string. What is the easiest way to pass the values?
<script type="text/javascript"> <!-- function foo() { var args = "start "; for( var i=0 ; i < arguments.length; i++ ) {
You call foo with *one* argument. That argument is an array
containing values, but it is only one array.
if(i==0) args += arguments[i];
Here you add the array to a string, so it is converted to a string.
Solutions:
function foo(arr) { // and change "arguments" to "arr"
or
alert(foo.apply(this,myArray1));
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
> I am trying to pass arrays of various lengths to a function. However the values are read as a string. What is the easiest way to pass the values?
<script> function foo() { var args = "start "; for( var i=0 ; i < arguments.length; i++ ) { if(i==0) args += arguments[i]; else if(i%2) // odd args += ' (' + arguments[i]; else args += ', ' + arguments[i] + ')'; }args += ' end'; return args; }
myArray1 = new Array('x', 2, 3) myArray2 = new Array('y', 5, 7, 11, 13)
alert(foo(myArray1)); // start 1,2,3 end alert(foo(myArray2)); // start 2,3,5,12,13 end
// alerts should be: start 1 (2, 3) end // start 2 (3, 5) (12, 13) end // --> </script>
You only use arguments when you have a variable number of arguments. In this
case, you have a single argument, which will be an array.
So replace the first line with
function foo(a) {
and replace all occurrences of 'arguments' with 'a'. http://www.crockford.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: dave.harper |
last post by:
I'm relatively new to C++, but have a question regarding functions and
arrays. I'm passing a relatively large array to a function several
thousand times during the course of a loop, and it seems...
|
by: kalinga1234 |
last post by:
there is a problem regarding passing array of characters to another
function(without using structures,pointer etc,).can anybody help me to
solve the problem.
|
by: Morgan |
last post by:
Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element...
|
by: Pete |
last post by:
Can someone please help, I'm trying to pass an array to a function, do some
operation on that array, then return it for further use. The errors I am
getting for the following code are, differences...
|
by: Steve Turner |
last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows:
typedef struct tag_scanparm
{
short...
|
by: John Pass |
last post by:
Hi,
In the attached example, I do understand that the references are not changed
if an array is passed by Val. What I do not understand is the result of line
99 (If one can find this by line...
|
by: sonaliagr |
last post by:
I am trying to update a msg array in function by passing the
address but it is showing an error. and also, i want the value of msg
array to be accessible to the full code that is inside the main...
|
by: =?Utf-8?B?U2hhcm9u?= |
last post by:
Hi Gurus,
I need to transfer a jagged array of byte by reference to unmanaged
function, The unmanaged code should changed the values of the array, and when
the unmanaged function returns I need...
|
by: Fizzics |
last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |