Given an array such as [0,1,2,3,4,5]
, where the length is always even.
How can I select and pair elements starting from the leftmost and rightmost positions all the way to the center?
For example, in the array above, it should result in [[0,5],[1,4],[2,3]]
I have already attempted a solution which can be seen here
const arr = [0,1,2,3,4,5]
const result = []
const possibleMatches = arr.length / 2
for (let x = 0; x < possibleMatches; x++) {
result.push([arr[x], arr[arr.length - x - 1]])
}
console.log(result)
//[ [ 0, 5 ], [ 1, 4 ], [ 2, 3 ] ]
However, are there better approaches than using a for loop? Perhaps utilizing one-line arrow functions?