Sreelatha
Sreelatha

Reputation: 31

Reorder array for desired output

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

Upvotes: 0

Views: 99

Answers (3)

maryf
maryf

Reputation: 150

function firstAndLast(array) { //You're declaring array here but you're using it in line 3 using the same array without, you're pasing myArray in line 12, my sugestion is to declare array inside de function
    var arr = [];
    array = myArray.sort().reverse();
    for(var i = 0; i < array.length; i++) {
        var firstItem = myArray[i];
        var lastItem = myArray[myArray.length - 1]; //You're always using the last item of your array, if i'm not wrong (or confused) you want decrement the position, right? you have to use myArray.length-i or a new variable to decrement the position

    if(lastItem > firstItem)
        arr.push(array[i]); //Again, you're using array when you want to use myArray (it is what you're using for the position in line 5 and 6)
}

var display = firstAndLast(myArray);

console.log(display);

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138235

Your code actually fits your description, except this part:

if(lastItem > firstItem){
        arr.push(array[i]);
}

Why don't you just push both items to the array:

if(lastItem > firstItem){
        arr.push(firstItem, lastItem);
}

And the lastItem should be dependent on i:

var lastItem = array[array.length - i - 1];

Them you only have to

return arr;

At the end and it should work :)

 function firstAndLast(array) {
    const result = [];
    array = array.sort((a, b) => a - b).reverse();
    for(var i = 0; i < array.length; i++){
       var firstItem = array[i];
       var lastItem = array[array.length - i - 1];
       if(lastItem < firstItem){
          result.push(firstItem, lastItem);
       }
    }
   return result;
}

 var myArray = [5,8,1,4,2,9,3,7,6];
 console.log(firstAndLast(myArray));

Now this only omits the value in the middle, which you can easily add like this in the loop:

   if(firstItem === lastItem) {
     result.push(firstItem);
   }

Upvotes: 1

Roberto Kedmenec
Roberto Kedmenec

Reputation: 324

Apparently you want to shuffle the array?

If that is the case the simplest way of doing it is just using this

function shuffle(array) {
  var m = array.length, t, i;

  // While there remain elements to shuffle…
  while (m) {

    // Pick a remaining element…
    i = Math.floor(Math.random() * m--);

    // And swap it with the current element.
    t = array[m];
    array[m] = array[i];
    array[i] = t;
  }

  return array;
}

It is Fisher-Yates shuffle.

More on it on the link : https://bost.ocks.org/mike/shuffle/

If that is not the case post a reply to the comment so we can find some new sorting logic!

Upvotes: 0

Related Questions