logo

Find the middle element

As a part of this Kata, you need to create a function that when provided with a triplet, returns the index of the numerical element that lies between the other two elements. The input to the function will be an array of three distinct numbers.


My code:


                        function gimme(triplet) {
                            let arr = [...triplet];
                            arr.sort((a, b) => a - b);
                            let middle = arr[1]
                            return triplet.indexOf(middle);
                          }                            
                    


And that's the best solution among all users:


                        function gimme(a) {
                            return a.indexOf(a.concat().sort(function(a, b) { return a - b })[1])
                          }