Created
May 10, 2016 19:49
-
-
Save alnorris/4e78ef10fa97f0857c344acfb7c3f488 to your computer and use it in GitHub Desktop.
Merge 2 sorted arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var arr1 = [3,5,8,10,15,18]; | |
var arr2 = [6,10,15,20,25]; | |
function sort(arr1, arr2) { | |
var sortedArr = []; | |
var x = y = 0; | |
while(x < arr1.length && y < arr2.length) { | |
if(arr1[x] < arr2[y]) { | |
sortedArr.push(arr1[x++]); | |
} | |
else { | |
sortedArr.push(arr2[y++]); | |
} | |
} | |
return sortedArr; | |
} | |
console.log(sort(arr1, arr2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment