# issue with merge sorted arrays problem using javascript

I am trying to solve merge 2 sorted arrays problem using javascript. Please find my solution below:

input:

const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];

output:

``````[1, 2, 3, 4, 7, 8, 9 10]

const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];

let arrayItem1 = arr1[0];
let arrayItem2 = arr2[0];
let i = 1;
let j = 1;
let mergedArray = [];

while(arrayItem2 || arrayItem1) {

arrayItem1 = arrayItem1 === undefined ? 0 : arrayItem1;
arrayItem2 = arrayItem2 === undefined ? 0 : arrayItem2;
if (arrayItem1 < arrayItem2) {
console.log('inside if');
console.log("first array", arrayItem1);
arrayItem1 = arr1[i];
i++;
mergedArray.push(arrayItem1);
} else {
console.log('inside else');
console.log("second array", arrayItem2);
arrayItem2 = arr2[j];
j++;
mergedArray.push(arrayItem2);
}

console.log("== merged array ==", mergedArray);
}
``````

But it is going in infinite loop. Not sure where I am going wrong. Need a watchful pair of eyes here.

thanks

### 21 thoughts on “issue with merge sorted arrays problem using javascript”

1. This will also work for non positive numbers

``````//[1, 2, 3, 4, 7, 8, 9 10]

const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];

let arrayItem1 = arr1[0];
let arrayItem2 = arr2[0];
let i = 1;
let j = 1;
let mergedArray = [];

while(arrayItem2 !== undefined || arrayItem1 !== undefined) {
if (arrayItem2 === undefined || arrayItem1 < arrayItem2) {
mergedArray.push(arrayItem1);
arrayItem1 = arr1[i];
i++;
} else {
mergedArray.push(arrayItem2);
arrayItem2 = arr2[j];
j++;
}
console.log('Merged array: ' + mergedArray)
}``````
2. Some issues:

• The actions in the `if` block (and `else` block) occur in the wrong order. You first want to push the item, then increment the index, and then get the next value from the array so it will be used in the next comparison.

• Don’t assign the value 0 when a value is undefined. Just leave it undefined, otherwise you risk to push a 0 into the result that was never there in the input.

So:

``````const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];

let arrayItem1 = arr1[0];
let arrayItem2 = arr2[0];
let i = 1;
let j = 1;
let mergedArray = [];

while(arrayItem2 || arrayItem1) {
if (arrayItem2 === undefined || arrayItem1 < arrayItem2) {
mergedArray.push(arrayItem1);
i++;
arrayItem1 = arr1[i];
} else {
mergedArray.push(arrayItem2);
j++;
arrayItem2 = arr2[j];
}
}
console.log("== merged array ==", mergedArray);``````
3. ``````const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];
const mergedArrays =  [...arr1, ...arr2];
// WITH SORT
const result = mergedArrays.sort((a, b) => Number(a) - Number(b));
console.log(result);

// WITHOUT SORT
const bubbleSort = (arr) => {
let done = false;
while (!done) {
done = true;
arr.forEach((el, i) => {
if (arr[i - 1] > arr[i]) {
done = false;
const tmp = arr[i - 1];
arr[i - 1] = arr[i];
arr[i] = tmp;
}
});
}

return arr;
};

const result2 = bubbleSort(mergedArrays)
console.log(result2)``````

You don’t really need to go through all that trouble, you can merge your arrays by destructuring your arrays in new one and just use the `Array.sort()` method.

UPDATE:

Added sorting without using using `Array.sort()`, using a sorting algorithm Bubble sort

4. `````` const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];
var children = arr1.concat(arr2);
console.log (children.sort(function(a, b){return a - b}));
``````
``children.sort(function(a, b){return a - b});``
`````` const arr1 = [1, 4, 7, 8, 10];
const arr2 = [2, 3, 9];
var children = arr1.concat(arr2);
console.log (children.sort(function(a, b){return a - b}));``````
5. You need to check the index with the lengths of the arrays and add a final check for getting the rest of the arrays added to the merged array.

``````const
array1 = [1, 4, 7, 8, 10],
array2 = [2, 3, 9],
mergedArray = [];

let i = 0,
j = 0;

while (i < array1.length && j < array2.length) {
if (array1[i] < array2[j]) {
mergedArray.push(array1[i++]);
} else {
mergedArray.push(array2[j++]);
}
}

if (i < array1.length) mergedArray.push(...array1.slice(i));
if (j < array2.length) mergedArray.push(...array2.slice(j));

console.log(...mergedArray);``````