Created
August 30, 2015 21:15
-
-
Save ironboy/d43e7894f0935b05ca0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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