Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    May 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help with array merge

    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.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    PHP Code:
    array3 array1.concat(array2);
    array3.sort(); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    May 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Old School

    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.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by whteout View Post
    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.

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

    Code:
    <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>
    Last edited by jmrker; 07-17-2011 at 04:32 PM.

  • #5
    New Coder
    Join Date
    May 2011
    Posts
    22
    Thanks
    5
    Thanked 0 Times in 0 Posts

    thanks

    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.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,078
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by whteout View Post
    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!

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,253
    Thanks
    12
    Thanked 341 Times in 337 Posts
    Quote Originally Posted by whteout View Post
    unfortunately I can not use the easy built in functions in JS.
    out of curiousity, why not?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,930
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Dormilich View Post
    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.

    Code:
    <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:-

    Code:
    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)
    Last edited by Philip M; 08-18-2011 at 08:19 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •