473,795 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing arrays to a function

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.lengt h; 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(myArr ay1)); // start 1,2,3 end
alert(foo(myArr ay2)); // start 2,3,5,12,13 end

// alerts should be: start 1 (2, 3) end
// start 2 (3, 5) (12, 13) end
// -->
</script>
Jul 20 '05 #1
2 29599
al*******@excit e.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.lengt h; 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.'
Jul 20 '05 #2
> 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.lengt h; 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(myArr ay1)); // start 1,2,3 end
alert(foo(myArr ay2)); // 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

Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10183
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 code... TCHAR myArray; DoStuff(myArray);
2
1971
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 to get bogged down. Do the arrays previously passed to the function stay memory resident? If not, what's causing it and what can I do to correct it? Thanks, Dave
8
4117
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.
2
4316
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 pointer. :-)) You were right about by mixed code: quickly writing an example to clarify the matter I made a C++ program, sorry for the inconvenience. Anyway, although it all works fine with monodimensional arrays, I still have a problem with...
10
3172
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 in levels of indirection, so I feel it must have something to do with the way I am representing the array in the call and the return. Below I have commented the problem parts. Thanks in advance for any help offered. Pete
2
4845
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 cmd; short fdc; WORD dsf; short boxcar; short average; short chan_ena;
11
8131
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
2
2196
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 function...I hope i am making sense...Please look at the code and help me in pointing out the error.. #include<stdio.h> #include<stdlib.h> #include<time.h>
17
7256
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 to show the array data to the end user. Can I do that? How?
1
4571
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 do, but because I can't seem to locate something that is even close... I am posting... I would like to thank this community, I have received a great deal of help already. Background: I have a pre-existing C code, that I have written, which works....
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.