Create your own array reverse() method in JavaScript


Took 21 minutes and 22 seconds to create array reverse() method in JavaScript. Here is the code snippet.

Code:
var arr = ['a', 'b', 'c', 'd', 'e'];

function reverse(arr) {
var arr2 = [];
var i = arr.length-1;
while (i >= 0) {
arr2.push(arr[i]);
i--;
}
return arr2;
}

document.write(reverse(arr));

Result:
e,d,c,b,a

1 comment:

Coding for Kids

What is coding? In coding we build a program to do a specific task for us. Code: A code is a set of computer instructions and when you will ...