Reverse an array in javascript without reverse function
No need to pop anything... Just iterate through the existing array in reverse order to make your new one. Show
Edit after answer got accepted. A link in a comment on your opening post made me test my way VS the accepted answer's way. I was pleased to see that my way, at least in my case, turned out to be faster every single time. By a small margin but, faster non the less. Here's the copy/paste of what I used to test it (tested from Firefox developer scratch pad): A typical
JavaScript challenge or interview question has to do with reversing arrays in-place and out-of-place. Specifically, take this problem from Eloquent JavaScript, 2nd Edition: Write two functions reverseArray and reverseArrayInPlace. The first, reverseArray, takes an array as an argument and produces a new array that has the same elements in the inverse order. The second, reverseArrayInPlace, does what the reverse method does: it modifies the array given as argument in order to reverse its elements. Neither may use the standard reverse method. That’s the kicker: you can’t use the I worked through this problem in the past, but now that I am getting back to studying JavaScript after a break, it took me awhile to re-wrap my head around it. Especially reversing the array in-place. To help me better understand and think through this problem I decided to write this article. Reversing an Array Out-of-PlaceTo start, let’s make up two arrays: var array1 = ["yes", "no", "maybe", "always", "sometimes", "never", "if"]; For the var newArray = []; Writing the steps out in pseudo-code:// 1) I want to take the last element of the array, then add it to the newArray. Let’s come up with our var i = array.length - 1 For example, Writing out our full for (var i = arr.length - 1; i >= 0; i--) In other words, we are subtracting 1 from
the index with each loop until function reverseArray(arr) {
Passing reverseArray(array1)⊳ ["if", "never", "sometimes", "always", "maybe", "no", "yes"] Reversing an Array In-PlaceThings immediately get more complicated for me when trying to reverse an array in-place. When I initially tried working through this problem I was trying to grab the first element using There was also the problem of where to start and end the Before going further, though, let’s write out the problem in pseudo-code: // 1) I know I need to loop through the array. But before figuring out where to start or end I need to think about how I will actually reverse the elements in the array. So in terms of our The way I initially thought to do this was: for (var i = 0; i <= (arr.length / 2); i++) I then set the first element equal to a variable called Here’s how it looks: let el = arr[i]; So with each loop,
Attempting it this way, it looks like: function reverseArrayInPlace(arr) { But in testing this, something odd happens with arrays that have an even
length, such as reverseArrayInPlace(array2) 9 and 5 are reversed in the middle! In cases where our array is an odd number, like To fix this we can subtract 1 from the length of the array before dividing it by 2. For good measure we can throw function reverseArrayInPlace(arr) { To review:
I’m a JavaScript rookie, so if you have a more efficient way of doing this I’d love to hear from you in the comments section below! function letsConnect(yes) {
How do you reverse an array without reverse function?Reverse Array Without using Reverse Function. class Program.. static void Main(string[] args). int[] arr = new int[] { 1, 2, 3, 4, 5 };. int length = arr.Length - 1;. string strReverse = null;. while (length >= 0). How do you reverse an element in an array without using the function?Steps to reverse an array without using another array in C:
Set i=0 to point to the first element and j=length-1 to point to the last element of the array. Run while loop with the condition i
How do you reverse an array in JavaScript?JavaScript Array reverse()
The reverse() method reverses the order of the elements in an array. The reverse() method overwrites the original array.
How many ways reverse array JavaScript?We can reverse an array by both modifying the source array or without modifying the source array.
|