1var arr = [34, 234, 567, 4];
2print(arr);
3var new_arr = arr.reverse();
4print(new_arr);
5
1// reversing an array in javascript is kinda hard. you can't index -1.
2// but i can show how you can do it in 4 lines.
3
4var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
5
6for (var i = myArray.length - 1; i > 0; i -= 1) {
7 myArray.shift();
8 myArray.push(i);
9}
10
11console.log(myArray); // output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
1const array1 = [1,2,3,4];
2console.log('array1:', array1);
3//"array1:" Array [1, 2, 3, 4]
4
5const reversed = array1.reverse();
6console.log('reversed:', reversed);
7//"reversed:" Array [4, 3, 2, 1]
8
9// Careful: reverse is destructive -- it changes the original array.
10console.log('array1:', array1);
11//"array1:" Array [4, 3, 2, 1]
1let array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
2let array2 = [5,8,2,9,5,6,3,1];
3
4function reverseArray(arr) {
5 var newArray = [];
6 for (var i = arr.length - 1; i >= 0; i--) {
7 newArray.push(arr[i]);
8 }
9 return newArray;
10}
11
12reverseArray(array1); // ["if", "never", "sometimes", "always", "maybe", "no", "yes"]
13reverseArray(array2); // [1, 3, 6, 5, 9, 2, 8, 5]
1var arr = [1, 2, 3, 4, 5, 6, 7];
2console.log (arr.reverse ());
3
4for (i = 0; i < arr.length / 2; i++) {
5 var temp = arr[i];
6 temp =arr[arr.length - 1 - i];
7 temp = temp;
8}
9console.log (arr);