Need Help

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. Must use a for and while loop and new array must stay ordered.

Is there any reason you can’t call Array’s sort() method on the new array once you’ve finished filling it with the contents of the two original arrays?

Because it might be some homework that he’s expected to do.

Normally you would use .concat() to merge two arrays, and the [url=“https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/sort”].sort() method to sort them in to numerical order.


var arr1 = [3, 7, 12, 15, 22, 45, 56];
var arr2 = [1, 2, 5, 17, 20];
var arrMerged = arr1.concat(arr2).sort(function (a, b) {
    return a - b;
});

Which gives: [1, 2, 3, 5, 7, 12, 15, 17, 20, 22, 45, 56]