Skip to content

Instantly share code, notes, and snippets.

@MohamedGamil
Created April 17, 2022 06:19
Show Gist options
  • Save MohamedGamil/9976b4c972518735f439a80ae66e9890 to your computer and use it in GitHub Desktop.
Save MohamedGamil/9976b4c972518735f439a80ae66e9890 to your computer and use it in GitHub Desktop.
HackerRank / Simple Array Sum
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'simpleArraySum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY ar as parameter.
*/
function simpleArraySum(ar) {
let sum = 0;
for (let num of ar) {
sum += num;
}
return sum;
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const arCount = parseInt(readLine().trim(), 10);
const ar = readLine().replace(/\s+$/g, '').split(' ').map(arTemp => parseInt(arTemp, 10));
const result = simpleArraySum(ar);
ws.write(result + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment