reverse elements in an array code example

Example 1: javascript reverse array

var arr = [34, 234, 567, 4];
print(arr);
var new_arr = arr.reverse();
print(new_arr);

Example 2: how to print array backwards

var array = ['a','b','c','d','e','f','g']
var j = array.length 

for(var i = 0; i < array.length ; i++){
    console.log(array[j])
    j=j-1 }

    /*

var j holds value of the array's number of values so every time in the loop it decrements by 1 making the 
function print a backwars array



    */

Example 3: reverse string array java

//java program to reverse array using for loop
public class ReverseArrayDemo 
{
   public static void main(String[] args) 
   {
      int[] arrNumbers = new int[]{2, 4, 6, 8, 10};  
      System.out.println("Given array: ");  
      for(int a = 0; a < arrNumbers.length; a++)
      {
         System.out.print(arrNumbers[a] + " ");
      }
      System.out.println("Reverse array: ");
      // looping array in reverse order
      for(int a = arrNumbers.length - 1; a >= 0; a--) 
      {  
         System.out.print(arrNumbers[a] + " ");  
      }
   }
}

Example 4: c++ reverse array

int arr[5] = {1, 2, 3, 4, 5}; //Initialize array

for(int i = 0; i < size(arr); i++) {
	//Create temporary variable to hold current value in array
	int temp = arr[i];
	//Set the current value in the array to the mirrored value in array
	arr[i] = arr[size(arr) - 1 - i];
	//Set mirrored value in array to temp, swapping the two numbers
	arr[size(arr) - 1 - i] = temp;
}

Example 5: reverse string array java

import java.util.Arrays;
public class ReverseStringArrayInJava
{
   public static void main(String[] args)
   {
      String[] strHierarchy = new String[]{"Junior Developer","Senior Developer","Team Lead","Project Manager","Senior Manager","CEO"};
      System.out.println("Given string array: " + Arrays.toString(strHierarchy));
      for(int a = 0; a < strHierarchy.length / 2; a++)
      {
         String strTemp = strHierarchy[a];
         strHierarchy[a] = strHierarchy[strHierarchy.length - a - 1];
         strHierarchy[strHierarchy.length - a - 1] = strTemp;
      }
      System.out.println("Reversed string array: ");
      for(int a = 0; a < strHierarchy.length; a++)
      {
         System.out.println(strHierarchy[a]);
      }
   }
}

Example 6: reverse array javascript

// reverse array with recursion function
function reverseArray (arr){
if (arr.length === 0){
return []
}
  return [arr.pop()].concat(reverseArray(arr))
}
console.log(reverseArray([1, 2, 3, 4, 5]))