HackerRank Arrays | JS Solution

HackerRank Arrays | JS Solution

Problem

HackerRank detailed problem description can be found here.

Inputs & Outputs

/*
input {array} arr
output {string}
*/

Test Case

arr = [1,2,3,4,5];

JavaScript Solution

function main() {
  const n = parseInt(readLine(), 10);

  const arr = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10));
  // Solution #1
  console.log(arr.reverse().toString().replace(/,/g, ' '));
  // Solution #2
  console.log(arr.reverse().join(' '));
}

Resources

  1. Arrays algorithm by HackerRank
  2. Array.prototype.toString() by MDN Web Docs
  3. String.prototype.replace() by MDN Web Docs
  4. Array.prototype.join() by MDN Web Docs