This works for everything but the num at the beginning and end of the array.
//Where do I belong
function getIndexToIns(arr, num) {
// Find my place in this sorted array.
arr.sort(); //sort the array
for (var a = 0; a < arr.length; a++){
if ( arr[a] >= num) {
return parseInt(a);
}
}
}
getIndexToIns([5, 3, 20, 3], 5);
Sometimes I find it helps to use the console.log to see what values certain things are returning. For instance your
arr.sort();
is returning:
[20, 3, 3, 5]
I’m not sure this is really what your looking for.
Therefore when you loop through the array and:
return parseInt(a)
you get:
0 & 3
which are the positions in the arr that meet your if statement.
Hi joelpeyton,
Thanks for the tip. I assumed that the table was sorted the way I wanted it and it was not.