Find the middle element

Lee·2022년 8월 31일

Algorithm

목록 보기
86/92
post-thumbnail

❓ Find the middle element

Q. 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 (Haskell: a tuple).

For example:

gimme([2, 3, 1]) => 0
2 is the number that fits between 1 and 3 and the index of 2 in the input array is 0.

Another example (just to make sure it is clear):

gimme([5, 10, 14]) => 1
10 is the number that fits between 5 and 14 and the index of 10 in the input array is 1.

✔ Solution

//#my solution
function gimme(triplet) {
  let test = [...triplet];
  let a = test.sort((a, b) => b - a)[1];
  return triplet.indexOf(a);
}

//#other solution
const gimme = function (arr) {
  return arr.indexOf([...arr].sort((x, y) => x > y)[1]);
};
profile
Lee

0개의 댓글