let arr = [1,2,3]
let newArr = arr.slice().reverse(); //returns a reversed array without modifying the original
console.log(arr, newArr) //[1,2,3] [3,2,1]
//The reverse() method reverses the elements in an array.
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
//output >> ["Mango", "Apple", "Orange", "Banana"]
//if you find the answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
const reverseString = (str) => {
const revArray = [];
const length = str.length - 1;
// Looping from the end
for(let i = length; i >= 0; i--) {
revArray.push(str[i]);
}
// Joining the array elements
return revArray.join('');
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)
#The original array
arr = [11, 22, 33, 44, 55]
print("Array is :",arr)
res = arr[::-1] #reversing using list slicing
print("Resultant new reversed array:",res)
/*method to reverse a linked list */
reverse(list) {
let current = list.head;
let prev = null;
let next = null;
if (this.head) {
//only one node
if (!this.head.next) { return this; }
while (current) {
next = current.next;//store next node of current before change
current.next = prev;//change next of current by reverse the link
prev = current;//move prev node forward
current = next;//move current node forward
}
list.head = prev
return list
}
return "is empty"
}
const reverseArray = (arr)=>{
for (let v = arr.length ; v > 0 ; v--) {
return arr[v];
}
}
//if you find this answer is useful ,
//upvote ⇑⇑ , so can the others benefit also . @mohammad alshraideh ( ͡~ ͜ʖ ͡°)