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)
// reversing an array in javascript is kinda hard. you can't index -1.
// but i can show how you can do it in 4 lines.
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = myArray.length - 1; i > 0; i -= 1) {
myArray.shift();
myArray.push(i);
}
console.log(myArray); // output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
/*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 ( ͡~ ͜ʖ ͡°)