In this program merged two array and then sorted using temp.but this not correct method.because two array are sorted ,so method should be unique i.e. merging of two sorted i
now you can do with es6 (...)spread operator
========================================
using es6 (...)spread operator
========================================
let a=[1,2,3,5,9]
let b=[4,6,7,8]
let sortedArray=[...a,...b].sort()
console.log(sortedArray)
outputy :- [1, 2, 3, 4, 5, 6, 7, 8, 9]
========================================
2nd way
========================================
function mergeSortedArray(a, b) {
var sortedArray = [], indexA = 0, indexB = 0;
while (indexA < a.length && indexB < b.length) {
if (sortFuntion(a[indexA], b[indexB]) > 0) {
sortedArray.push(b[indexB++]);
} else {
sortedArray.push(a[indexA++]);
}
}
if (indexB < b.length) {
sortedArray = sortedArray.concat(b.slice(indexB));
} else {
sortedArray = sortedArray.concat(a.slice(indexA));
}
return sortedArray;
}
function sortFuntion(a, b) {
return a - b;
}
console.log(mergeSortedArray([1,2,3,5,9],[4,6,7,8]));
output :- 1,2,3,4,5,6,7,8,9