Forum Moderators: open

Message Too Old, No Replies

Help with Arrays

         

zero3ree

9:00 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



I am venturing into arrays for the first time and can't quite understand it. I need to create a function that goes through elements of array and square them from another function.

Do i need to make an array or does a variable with "[]" do it?

Here is what I have and it says element is undefined.

JS:
function applyFunction(arr,func){
for(var arr=0;arr<=4;arr++);

}
function square(element) {
return element * element;
}

HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script src="apFunc.js" language="JavaScript" type="text/javascript"></script>

</head>
<script>
var arr = [1,2,3,4];
var arr1=applyFunction(arr,square);
document.write(arr1);
</script>
<body>
</body>
</html>

Thanks for any help.

Fotiman

9:35 pm on Nov 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



An array is an ordered collection of items. It has a length (how many items are in the array) and each item in the array can be referenced by an "index" into the array. Note, JavaScript indexes begin at zero.

For example:

var arr = [1,2,3,4];

That is an array containing 4 number items. The item at index 0 is the number 1. The item at index 1 is the number 2, and so on.

You applyFunction method needs a little tweaking. First, you'll want a variable to store the return value (this is going to be an array of the same size as the array that was passed in, and we're going to leave the original array unchanged):


function applyFunction(arr, func) {
var result = [];
//...
return result;
}

Next, you want to loop through the array that was passed in:


var i, n = arr.length;
for (i = 0; i < n; i++) {
//...
}

Combine that with what we already had...

function applyFunction(arr, func) {
var result = [], i, n = arr.length;
for (i = 0; i < n; i++) {
//...
}
return result;
}

For each item in the array, you want to call the passed in function with that item as a parameter, and assign the return result to the corresponding index in the new array:

function applyFunction(arr, func) {
var result = [], i, n = arr.length;
for (i = 0; i < n; i++) {
result[i] = func(arr[i]);
}
return result;
}

That's it. :)

Side Note: don't include the language="JavaScript" attribute on your script tag. It's invalid and is not needed. Also note, in general it's best to avoid using document.write... instead use DOM methods to append to the DOM or assign values to innerHTML of elements.

zero3ree

10:34 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



Thank you very much.

zero3ree

11:35 pm on Nov 19, 2009 (gmt 0)

10+ Year Member



I also am trying to add two arrays together. I know that you can use join or concat but is there any other options.

Something like this:
var arr1 = [1, 2, 3, 4];
var arr2 = [5, 6, 7, 8];
appendArray(arr1, arr2);

where the appendArray creates this [1,2,3,4,5,6,7,8]

can I add the second array to the index of arr1 in the function or is that reserved for only adding a new element?

Fotiman

12:25 am on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, you could combine two arrays, much like how I created the results array above. Give it a try and if you have questions, feel free to ask. Here's a hint to get you started...
If you don't want to modify either of the two input arrays, you'll need to create a new array by first looping through one array and copying the items to the results array, then doing the same thing for the second array... but make sure to adjust the index so that you don't overwrite the data you copied from the first array.
If you want to modify the first array, then just loop through the second array and add each item to the end of the first array (note, the length property of an array can be used to append to the end of the array like so:
arr[arr.length] = value;

zero3ree

2:08 am on Nov 20, 2009 (gmt 0)

10+ Year Member



Ill be honest. I really am having trouble with this copying array. I started this but I know it probably isn't close

function appendArray(ar1, ar2){
var result = [], i, n = ar1.length;
for (i = 0; i < n; i++) {
result[i] = (ar1[i]+ ar2[i]);
}
document.write(result[i]);

}

Thanks for the help I'm stuck on this.

astupidname

3:20 am on Nov 20, 2009 (gmt 0)

10+ Year Member



What's wrong with doing:
var arr1 = [0, 1, 2, 3, 4];
var arr2 = [0, 5, 6, 7, 8];

var myNewArr = [].concat(arr1,arr2);
alert(myNewArr);

zero3ree

4:01 am on Nov 20, 2009 (gmt 0)

10+ Year Member



I was tasked with creating it without using the easy route. I am new to this so I can't figure out the way it needs to be done.

zero3ree

4:09 am on Nov 20, 2009 (gmt 0)

10+ Year Member



I just figured it out after reading your post a few more times.:)

Thank you for all your help.

Fotiman

2:09 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Glad you got it working. Care to post your final version?

zero3ree

3:10 pm on Nov 20, 2009 (gmt 0)

10+ Year Member



No problem I am at work right now I will when I get home.