0
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits = fruits.sort();
document.write(fruits);
for(var i = 0; i < Math.ceil(fruits.length / 2); i++) {
    var temp = fruits[i];
    fruits[i] = fruits[fruits.length - 1 - i];
    fruits[fruits.length - 1 - i] = temp;
}
document.write(fruits);

I'm trying to make a reverse of the ordered (a-z) array fruits, in order to make it z-a in the for loop. Why isn't this working?

2
  • We don't vote to close when a solution's been found. That's reserved for off-topic or inappropriate questions, etc. Commented Dec 23, 2010 at 22:11
  • With your rep, you should be able to close your own question. But better yet, post your solution, and then in a couple of days mark that as the accepted answer (if Patrick's doesn't do it for you). Commented Dec 23, 2010 at 22:14

1 Answer 1

6

Just use .reverse().

fruits.reverse();
Sign up to request clarification or add additional context in comments.

3 Comments

Too easy :D No, but I want to do it like this.
@DarkLightA then tough cookies.
Got it. Two errors: firstly I used ceil() instead of Math.ceil(), and I forgot to print the outcome!

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.