hackerrank

Simple Array Sum solution using JavaScript

Below is my solution to the Simple Array Sum challenge on HackerRank. In order to pass the tests, you'll need to iterate through the array of integers and sum them up.

I chose to do this using the reduce method. You'll then need to print the resulting sum to the console.

function main() {  
    let n = parseInt(readLine());
    let arr = readLine().split(' ').map(Number);
    let sum = arr.reduce((acc, number) => acc + number, 0);

    console.log(sum);

    return sum;
}

more HackerRank posts

more JavaScript posts