The goal of this code is to sort this array [1,2,3,4,5] to look like this [5,3,1,2,4]. I also want it to sort arrays that someone else sends me, so for example it should work with [-10,-1,-4] as [-1,-10,-4]. The array should be sorted using this rules:
- Smallest number is in the middle.
- Number higher than the smallest one should be right to it [2](In first example).
- Number higher than [2](In the first example) should be left from [1](In the first example).
So far this is my idea:
let values = [1,2,3];
let storage = values.sort((a, b) => a - b);
let result = storage[0];
for( let i = 1; i <= (values.length/2); i+=2 ){
result.unshift (storage[i]);
result.push (storage[i+1]);
}
return result);
But I can't make it work, I'm lost on what I can change at the moment.