Help-me || JavaScript

Write a function that takes as an argument an array and orders it similar to the movement of a pendulum, that is:

The smallest element in the array must be in the center, the second smallest after the smallest and the third smallest before the smallest.

# Examples:
pendulum ([1, 2, 3]) // => [3, 1, 2]
pendulum ([6, 6, 8, 5, 10]) // => [10, 6, 5, 6, 8]
pendulum ([- 10, 0, 2, 4, 5]) // => [5, 2, -10, 0, 4]

# Restrictions:
The array has at least 3 elements and will always have an odd size.
Numbers in array include positives and negatives.

/**
 * Your sorting function
 */
const sort = a => a.sort()
const array = sort([ 5, 4, 3, 2, 1 ])

let result = []
for (let i = 0; i < array.length; i++) {
  if (i % 2 === 0) result.unshift(array[i])
  else result.push(array[i])
}

console.log(result)
2 Likes