Reversable array

How to reverse the array numbers?
see my code below and how to show the output.

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>

    <p id="demo"></p>

      var reverse = function(arr) {
       var result = [],
           ii = arr.length;
       for (var i = ii - 1; i !== 0; i--) {
       return result;
    document.getElementById("demo").innerHTML = result; 

Array.reverse() ?

1 Like

Hi Dormilich,

Thanks for the reply.

I want how to reverse the array numbers wit out using “reverse()” method?


do you have idea please help me with the code?

check your error console. there should be a message.

and you don’t have an array to reverse.

var data = [1, 2, 3, 4];

var reverse = function (arr) {
    var result = [],
        i = arr.length - 1;

    do {
    } while (i--);
    return result;


Depending on your environment you could make use of Array.prototype.reduceRight.

console.log(data.reduceRight(function (carry, item) {
    return carry;
}, []));

Or go all out with arrow functions.

console.log(data.reduceRight((c, i) => { c.push(i); return c; }, []));

[quote=“koder, post:3, topic:207798, full:true”]I want how to reverse the array numbers wit out using “reverse()” method?

Hang on - why are you not wanting to use the built-in technique of reversing arrays?

After all, it’s as easy as this, and works all browsers, for a given value of all that includes everything from IE5.5 (ancient and historical) onwards.

var data = [1, 2, 3, 4];
document.getElementById("demo").innerHTML = data.reverse();
// reversed array is all done

To me it smells a little bit like homework.

Write a function that reverses the contents of a given array. You are not allowed to use Array.reverse in your solution"

I could be wrong though, hence why I knocked up a quick solution for the OP. (Which has a nasty bug in it :slight_smile: )

This may be equally as easy. :smile:

<script type="text/JavaScript">
 var orig=[1,2,3,4], result=[];
 for(var i=orig.length-1; i>-1; i--)
  { result[result.length]=orig[i]; }
 alert("result= ["+result.join()+"]"); 

How is that as equally easy as orig.reverse() ?

var orig = [1, 2, 3, 4];
console.log('Reversed array: ', orig.reverse());

Normally the .reverse() method reverses the actual array.

If instead you don’t want the original array to be changed, this can be done by making a separate copy of an array, it can be put together with no side-effects as follows:

function cloneArray(arr) {
    // Make a separate copy of the array
    return arr.slice(0);
function reverseArray(arr) {
    // As using arr.reverse() would also result in the original array being reversed,
    // a separate copy is made of the array first before returning the reversed result.
    return cloneArray(arr).reverse();

var orig = [1, 2, 3, 4];
console.log('Reversed array: ', reverseArray(orig);

or more simply:

var orig = [1, 2, 3, 4];
console.log('Reversed array: ', orig.slice(0).reverse();

As I understand it the OP did not want to use reverse().

Even without using the arrow the code isn’t that much longer (it looks longer here because of the different font)

function reverse(data) {
return  data.reduceRight(function (c, i) { c.push(i); return c; }, []);

much simpler than the other versions and still has the advantage over Array.reverse() of producing a new array instead of overwriting the original.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.