HackerRank process.stdout.write问题

时间:2018-07-02 03:58:46

标签: node.js

我一直在尝试学习如何使用HackerRank提供的IDE,但是提交答案时遇到了问题。问题here让您将数组中的所有值加到一个整数中。我遇到的问题是我的代码正在生成正确的值,但不会从数字转换为整数或在要测试的stdout中注册。代码如下。

'use strict';

const fs = require('fs');

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(str => str.trim());

    main();
});

function readLine() {
    return inputString[currentLine++];
}

/*
 * Complete the simpleArraySum function below.
 */


// my code 
function simpleArraySum(ar) {

    const reducer = (accumulator, currentValue) => accumulator + currentValue;
    var sumOfArr = ar.reduce((reducer))
    console.log(sumOfArr); 
    process.stdout.write(sumOfArr)
}
//  end of my code 
function main() {

    const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

    const arCount = parseInt(readLine(), 10);

     const ar = readLine().split(' ').map(arTemp => parseInt(arTemp, 10));

     let result = simpleArraySum(ar);

     ws.write(result + "\n");

     ws.end();
}

0 个答案:

没有答案