var data = [1, 2, 3, 4];
var reverse = function (arr) {
var result = [],
i = arr.length - 1;
do {
result.push(arr[i]);
} while (i--);
return result;
};
console.log(reverse(data));
[quote=“koder, post:3, topic:207798, full:true”]I want how to reverse the array numbers wit out using “reverse()” method?
[/quote]
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
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);