Skip to content

Commit afed561

Browse files
Add reverse.js
1 parent 3292e5b commit afed561

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

reverse.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
*
3+
* @param {Array} arr array of elements
4+
* @returns {Array} a new array with the elements in the inverse order
5+
*/
6+
function reverse(arr) {
7+
if (arr.length === 0) return []
8+
const x = arr[0]
9+
const xs = arr.slice(1)
10+
return reverse(xs).concat([x])
11+
}
12+
13+
console.log(reverse([1,2,3]))
14+
console.log(reverse([]))

0 commit comments

Comments
 (0)