reverse a 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: array reverse algorithm in js
let array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"];
let array2 = [5,8,2,9,5,6,3,1];
function reverseArray(arr) {
var newArray = [];
for (var i = arr.length - 1; i >= 0; i--) {
newArray.push(arr[i]);
}
return newArray;
}
reverseArray(array1);
reverseArray(array2);
Example 3: reverse array javascript
var rev = arr.reverse();
Example 4: reverse array in javascript
const reverseArray = arr => arr.reduce((acc, val) => [val, ...acc], [])
Example 5: Reversing an array
#include<iostream>
using namespace std;
int main()
{
int Arr[100],n,temp,i,j;
cout<<"Enter number of elements you want to insert ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Enter element "<<i+1<<":";
cin>>Arr[i];
}
for(i=0,j=n-1;i<n/2;i++,j--)
{
temp=Arr[i];
Arr[i]=Arr[j];
Arr[j]=temp;
}
cout<<"\nReverse array"<<endl;
for(i=0;i<n;i++)
cout<<Arr[i]<<" ";
return 0;
}