merge sorted arrays

Run Settings
LanguageJavascript
Language Version
Run Command
function mergeSortedArrays(array1,array2) { const mergedArray = []; //Check input if(array1.length === 0) { return array2; } if(array2.length === 0) { return array1; } if(array1.length === 0 && array2.length === 0) { return mergedArray; } let array1Item = array1[0]; let array2Item = array2[0]; let array1Index = 1; let array2Index = 1; while(array1Item || array2Item) { if(!array2Item || array1Item < array2Item) { mergedArray.push(array1Item); array1Item = array1[array1Index]; array1Index++; } else { mergedArray.push(array2Item); array2Item = array2[array2Index]; array2Index++; } } return mergedArray; } console.log(mergeSortedArrays([0,3,4,31],[4,6,30]));
Editor Settings
Theme
Key bindings
Full width
Lines