JSFiddle - React, Tailwind, and code Playground

by prozoroff

JavaScript

const arr1 = [1, 2, 3];
const arr2 = [2, 6, 9];

const merge = (a, b) => {
	const result = [];
	let indA = 0;
	let indB = 0;
	let maxA = a[indA];
	let maxB = b[indB];
	const resultLength = a.length + b.length;

	while (result.length < resultLength) {
		while (indA < a.length && (a[indA] <= maxB || indB >= b.length)) {
			result.push(a[indA]);
			indA += 1;
		}
		maxA = a[indA];

		while (indB < b.length && (b[indB] <= maxA || indA >= a.length)) {
			result.push(b[indB]);
			indB += 1;
		}
		maxB = b[indB];
	}

	return result;
};

console.log(undefined > 2);