[英]Code for reversing an array in JavaScript fails
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 (az) array fruits, in order to make it za in the for loop. 我正在尝试使有序(az)数组结果相反,以使其在for循环中成为za。 Why isn't this working? 为什么这不起作用?
fruits.reverse();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.