Skip to content

Instantly share code, notes, and snippets.

@ironboy
Created August 30, 2015 21:15
Show Gist options
  • Select an option

  • Save ironboy/d43e7894f0935b05ca0d to your computer and use it in GitHub Desktop.

Select an option

Save ironboy/d43e7894f0935b05ca0d to your computer and use it in GitHub Desktop.
/*
Solution to
https://blog.svpino.com/2015/05/24/programming-challenge-the-position-of-the-element
*/
function posOfEl(list,el){
// if the element exist return the index
var index = list.indexOf(el);
if(index>=0){return index;}
// otherwise insert it in a copy
// of the list, sort it and return the index
list = list.slice();
list.push(el);
return list.sort(function(x,y){
return x < y ? -1 : 1;
}).indexOf(el);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment