开发者

Code for reversing an array in JavaScript fails

开发者 https://www.devze.com 2023-02-01 14:06 出处:网络
var fruits = [\"Banana\", \"Orange\", \"Apple\", \"Mango\"]; fruits = fruits.sort(); document.write(fruits);
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 reve开发者_C百科rse of the ordered (a-z) array fruits, in order to make it z-a in the for loop. Why isn't this working?


Just use .reverse().

fruits.reverse();
0

精彩评论

暂无评论...
验证码 换一张
取 消