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
- Arrays algorithm by HackerRank
- Array.prototype.toString() by MDN Web Docs
- String.prototype.replace() by MDN Web Docs
- Array.prototype.join() by MDN Web Docs