I got an array
var myArray = [5,8,1,4,2,9,3,7,6];
I want the output to be [ 9, 1, 8, 2, 7, 3, 6, 4, 5 ]. I tried the following code:
function firstAndLast(array) {
var arr= [];
array = myArray.sort().reverse();
for(var i = 0; i < array.length; i++){
var firstItem = myArray[i];
var lastItem = myArray[myArray.length - 1];
if(lastItem > firstItem){
arr.push(array[i]);
}}
var display = firstAndLast(myArray);
console.log(display);
Can anyone suggest what am I missing to achieve the targeted result?
What I want to acheive is to arrange the array in even odd indexes where odd indexes contain larger values in descending order and even indexes contain values in ascending order
returnanything.var res = [];for(var i=1;i<5; i++){res.push(10 - i);res.push(i);}res.push(5);.If it's always the case and you want to get items this way.