merge sorted

by TheDiamondDoge

JavaScript

let x = [1,3,5,7,9];
let y = [2,4,5,6,8,10];

function mergeSorted(a, b) {
	let x = [...a];
  let y = [...b];
	let result = [];
  
	while(x.length > 0 && y.length > 0) {
  	if (x[0] < y[0]) {
    	result.push(x.shift());
    } else {
    	result.push(y.shift());
    }
  }
  
  return [...result, ...x, ...y];
}

console.log(mergeSorted(x, y));
console.log(x, y);