Skip to content

Instantly share code, notes, and snippets.

@chrsr
Last active December 14, 2015 22:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chrsr/5157181 to your computer and use it in GitHub Desktop.
A CodePen by Chris Roberts.
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