...

View Full Version : Help with array merge



whteout
07-17-2011, 05:52 AM
not sure I understand the concept behind merging two arrays together. I know that I have to make a deep copy of each array before applying the function. I know that I have to create a temp value to store the information in the arrays while I insert, but not sure how to keep in numeric order. array 1 = [3, 7, 12, 15, 22, 45, 56] and array 2 = [1, 2, 5, 17, 20] What simple code could I start with to create this.

Dormilich
07-17-2011, 04:38 PM
array3 = array1.concat(array2);
array3.sort();

whteout
07-17-2011, 04:52 PM
unfortunately I can not use the easy built in functions in JS. I have to iterate thru first array and add the 2nd array to it to create the 3rd array.

jmrker
07-17-2011, 05:29 PM
unfortunately I can not use the easy built in functions in JS. I have to iterate thru first array and add the 2nd array to it to create the 3rd array.

I don't understand why you cannot use concat() function. :confused:

If you don't like that elegant solution, you probably won't like this as well.



<script type="text/javascript">

var array1 = [3, 7, 12, 15, 22, 45, 56];
var array2 = [1, 2, 5, 17, 20];
var array3 = [];
for (var i=0; i<array1.length; i++) { array3.push(array1[i]); }
for (var i=0; i<array2.length; i++) { array3.push(array2[i]); }
alert(array1+'\n\n'+array2+'\n\n'+array3);
</script>

whteout
07-17-2011, 05:41 PM
I am trying to find a simple solution using a for and while loop to make this work. I have it started, but not sure how the for and while loop should look like.

jmrker
07-17-2011, 06:24 PM
I am trying to find a simple solution using a for and while loop to make this work. I have it started, but not sure how the for and while loop should look like.

You should post your attempts as well as your question when you post.
That way you will get an answer that is more appropriate to your knowledge level!
;)

Dormilich
07-17-2011, 07:12 PM
unfortunately I can not use the easy built in functions in JS.
out of curiousity, why not?

Philip M
08-17-2011, 08:52 PM
out of curiousity, why not?

Homework of course! :) As I am in a good mood I will do it for you, on the understanding that you study the code and learn from it.

Note that the sort() function proposed by dormilich sorts into lexographical (alphabetic) order with the result

1,12,15,17,2,20,22,3,45,5,56,7

You need to sort into numerical order, which means using your own sort order function.


<script type = "text/javascript">

var array1 = [3, 7, 12, 15, 22, 45, 56];
var array2 = [1, 2, 5, 17, 20];

var len1 = array1.length;
var len2 = array2.length;
var tlen = len1+len2
for (var i = 0 ; i<len2; i++) {
array1[i+len1] = array2[i];
}

array1.sort(AscendingNumberSort);
document.write(array1); // 1,2,3,5,7,12,15,17,20,22,45,56

function AscendingNumberSort (a,b) {return a-b;}

</script>


If you are not allowed to use the inbuilt sort() function, Google for "Javascript bubble sort" and use/adapt that. To save you the trouble:-


var temp;
for (var x = 0; x < array1.length; x++) {
for(var y = 0; y < (array1.length-1); y++) {
if(array1[y] > array1[y+1]) {
temp = array1[y+1];
array1[y+1] = array1[y];
array1[y] = temp;
}
}
}
document.write(array1); // 1,2,3,5,7,12,15,17,20,22,45,56


“A man ceases to be a beginner in any given science and becomes a master in that science when he has
learned that he is going to be a beginner all his life.” - Robin G. Collingwood (English Philosopher, 1889-1943)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum