HackerRank Solution in ES6 Javascript to Challenge: Arrays – DS

I just started to complete challenges over at HackerRank and this is the first challenge under Data Structures > Arrays. I will be posting my solutions as I compete them. (Whenever I have time to actually work on them, haha)

Arrays – DS

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A, of size N, each memory location has some unique index, i (where 0 <= i <= N), that can be referenced as A[i] (you may also see it written as Ai).

Input Format

The first line contains an integer, N (the number of integers in A).
The second line contains N space-separated integers describing A.


  • 1 <= N <= 103
  • 1 <= Ai <= 104, where Ai is the ith integer in A

Output Format

Print all N integers in A in reverse order as a single line of space-separated integers.

Sample Input

1 4 3 2

Sample Output

2 3 4 1

My Solution to this Challenge

I submitted my answer using Javascript ES6, but below I give you three solutions, one in ES5 and two in ES6


var arr = [1,2,3,4];
var string = '';
for (var i=arr.length-1; i>=0; i--) {
	string = string.concat(arr[i]) + ' '


4 3 2 1


Using ‘forEach’ Helper

let arr = [1,2,3,4];
let string = '';
arr.reverse().forEach(item => string = `${string} ${item}`)

Using ‘reduce’ Helper

let arr = [1,2,3,4];
let string = '';
arr.reverse().reduce((prev, item) => string = `${string} ${item}`,0);


4 3 2 1

Open solution in HackerRank


Esau Silva
Software Engineer at Region One ESC
Full Stack Software Engineer working with Microsoft technologies, ReactJS is awesome and avid Brazilian Jiu-Jitsu practitioner
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed.