Created
January 20, 2013 23:26
-
-
Save atroche/4582554 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var insertAtRightPlace = function(number, array) { | |
var i; | |
for(i = 0; i < array.length; i++) { | |
if (number <= array[i]) { | |
break; | |
} | |
} | |
array.splice(i, 0, number); | |
}; | |
var merge = function(arrayOfArrays) { | |
var i, j, array, newNumber, newArray = []; | |
for(i = 0; i < arrayOfArrays.length; i++) { | |
array = arrayOfArrays[i]; | |
for(j = 0; j < array.length; j++) { | |
newNumber = array[j]; | |
insertAtRightPlace(newNumber, newArray); | |
} | |
} | |
return newArray; | |
}; | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('merge', function(){ | |
it('flattens an array of already sorted arrays and maintains order', function(){ | |
var arrayOfArrays = [[0, 5, 8, 9], [1, 2, 7], [10]]; | |
expect(merge(arrayOfArrays)).toEqual([0, 1, 2, 5, 7, 8, 9, 10]); | |
}); | |
}); | |
describe('insertAtRightPlace', function(){ | |
it('takes a number and an array and inserts the num so as to maintain order', function(){ | |
var number = 7, array = [1, 4, 5, 8, 9, 11]; | |
insertAtRightPlace(number, array); | |
expect(array).toEqual([1, 4, 5, 7, 8, 9, 11]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment