Back to Type Challenges

README

questions/09896-medium-get-middle-element/README.md

latest1.5 KB
Original Source
<!--info-header-start--><h1>GetMiddleElement </h1><blockquote><p>by 凤之兮原 <a href="https://github.com/kongmingLatern" target="_blank">@kongmingLatern</a></p></blockquote><p><a href="https://tsch.js.org/9896/play" target="_blank"></a> &nbsp;&nbsp;&nbsp;<a href="./README.zh-CN.md" target="_blank"></a> </p><!--info-header-end-->

Get the middle element of the array by implementing a GetMiddleElement method, represented by an array

If the length of the array is odd, return the middle element If the length of the array is even, return the middle two elements

For example

ts
  type simple1 = GetMiddleElement<[1, 2, 3, 4, 5]>, // expected to be [3]
  type simple2 = GetMiddleElement<[1, 2, 3, 4, 5, 6]> // expected to be [3, 4]
<!--info-footer-start-->

<a href="../../README.md" target="_blank"></a> <a href="https://tsch.js.org/9896/answer" target="_blank"></a> <a href="https://tsch.js.org/9896/solutions" target="_blank"></a> <!--info-footer-end-->