Javascript Array mergeArrays(arr1, arr2)

Description

Javascript Array mergeArrays(arr1, arr2)


var mergeArrays = function(arr1, arr2) {
  var result = [];

  while (arr1.length && arr2.length) {
    if (arr2[0] >= arr1[0]) {
      result.push(arr1.shift());//from ww  w  . j a  v a 2  s .  c  om
    }
    else if (arr1[0] > arr2[0]) {
      result.push(arr2.shift());
    }
  }

  while (arr1.length)
    result.push(arr1.shift());

  while (arr2.length)
    result.push(arr2.shift());

  return result;
};



PreviousNext

Related