How to reverse a array? How to reverse a array in javascript? - letsbug
Hey everyone in this article we're going to see how we can reverse a array. In this article I am using javascript but it's not important. You can use any language you want only thing which is important is the logic behind it. You understand the logic means you can do or implement this in any language.
In the below code I start by first creating a arr variable and add random 10 numbers as the elements in the array. Actual reversing of the array happens in the reverseArray(array, n) function.
In the function we first create a variable and initialize it to a array to store the reversed array. The we loop the original array backwards starting from the last element to the first element. While passing through each element we push that element to the start of our reverserdArray variable. After the loop terminates we have the reversed version of original array returned from the function.
After that we can call the function and check the console to see the output. That's it.
Below is the code in javascript.
Comments
Post a Comment