public
Last active

A CodePen by Chris Roberts.

  • Download Gist
script.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.