let firstList = [4, 6, 8, 9]
let secondList = [2, 3, 5, 7]
function getSmallestThenRemove(firstList, secondList) {
let smallestFirstList = firstList[0];
let smallestSecondList = secondList[0];
if (smallestFirstList < smallestSecondList) {
return firstList.shift()
} else {
return secondList.shift()
}
}
function mergeLists(firstList, secondList) {
let newList = [];
let iteratedNum;
while (firstList.length != 0 && secondList.length != 0) {
let iteratedNum = getSmallestThenRemove(firstList, secondList)
newList.push(iteratedNum)
}
return newList.concat(firstList).concat(secondList)
}
console.log(mergeLists(firstList, secondList));
Created
July 10, 2018 16:27
-
-
Save andrit/7d01e358604626315d9e2ea297eaef4d to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment