Last active
December 14, 2015 22:18
-
-
Save chrsr/5157181 to your computer and use it in GitHub Desktop.
A CodePen by Chris Roberts.
This file contains 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
var myobjs = [ | |
{position: null}, | |
{position: null}, | |
{position: 5}, | |
{position: 1}, | |
{position: null}, | |
{position: 6}, | |
{position: 3}, | |
{position: 10}, | |
{position: 2}, | |
{position: 11}, | |
{position: null}, | |
{position: 4}, | |
{position: null}, | |
{position: 15}, | |
{position: null}, | |
{position: 0} | |
]; | |
var min, max, available, i, index = 0, found, numbers = []; | |
// http://ejohn.org/blog/fast-javascript-maxmin | |
Array.max = function( array ){ | |
return Math.max.apply( Math, array ); | |
}; | |
Array.min = function( array ){ | |
return Math.min.apply( Math, array ); | |
}; | |
// Sorting function | |
function compareNumbers(a, b) { | |
return a - b; | |
} | |
// get only the numbers | |
$.each(myobjs, function(key, it) { | |
if (it.position) { | |
numbers.push(it.position); | |
} | |
}); | |
numbers.sort(compareNumbers); | |
min = Array.min(numbers); // if min is not 1 then return 1 perhaps? | |
max = Array.max(numbers); | |
for (i = min; i < max; i++) { | |
if (i !== numbers[index]) { | |
available = index + 1; | |
break; | |
} | |
index++; | |
} | |
console.log(available); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment