給定一數組,將其元素倒序排列並輸出。數組
// 利用函數翻轉任意數組 reverse 翻轉 function reverse(arr) { var newArr = []; for (var i = arr.length - 1; i >= 0; i--) { newArr[newArr.length] = arr[i]; } return newArr; } var arr1 = reverse([1, 3, 4, 6, 9]); console.log(arr1); var arr2 = reverse(['red', 'pink', 'blue']); console.log(arr2);
[9, 6, 4, 3, 1]
["blue", "pink", "red"]