For people who like perl, assembler or regular expressions, here is the solution next to the single-line layer:
var compV = 1; var scores=[0.7, 1.05, 0.81, 0.96, 3.2, 1.23]; for(var h=0,l=0,i=0,tmp=0,lV=Number.MAX_VALUE,hV=lV; i < scores.length; i++) { tmp=compV-scores[i];tmp>0 && tmp<lV ? (l=i,lV=tmp) : tmp<0 && -tmp<hV ? (h=i,hV=-tmp) : 0; }
l (lowercase "L") contains the lowest value index, h (lowercase "H") contains the highest value index. Good luck :)
f6a4 source share