0

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

3
  • Your function doesnt return anything. Commented Jul 24, 2018 at 13:42
  • Well if that's what you need, you can just do: 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. Commented Jul 24, 2018 at 14:23
  • You shouldn't remove a question's content once people have spent time helping you (in this case, a LOT of time!) - that is against the site guidelines. Instead, you should mark the person's contribution as "the" answer by clicking the checkmark next to it to thank them. Commented Jul 24, 2018 at 19:21

3 Answers 3

1

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);
   }
Sign up to request clarification or add additional context in comments.

2 Comments

Another problem is that it will only work properly for numbers <= 9 (remember sort sorts lexicographicly).
I fixed it. The push should be firstItem, lastItem. And it worked. Thank you so much. I will try to optimize the code with ES6 now.
0

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!

7 Comments

@sjahan, I've posted it before he did the edit. Ill help him now with the edit.
It's not about the edit: OP does not want to shuffle. Though, it appears it's not clear yet what he really wants to do. Don't post answer without being sure of understanding the question, or you'll probably get flagged.
@sajah The best thing I've could figured out is he wanted to shuffle the array. Since he wasn't responding or editing the question i tought he want to shuffle it. Br,Kedmenec.
I totally understand, though, it's a bad SO practice to answer if you're not sure about the question!
@sjahab are you sure about that. There are many situations where answerers take a guess. Many time even the op doesn't know what is the problem till they get the answer.
|
0
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.