如何 运行 来自 HackerRank on Idea / WebStorm IDE 的 JavaScript 解决方案?

How to run a JavaScript solution from HackerRank on Idea / WebStorm IDE?

假设您想在 WebStorm / IntelliJ Idea IDE 上针对 macOS 运行 / 本地调试 HackerRank 解决方案,然后再提交。考虑到 node.js 已安装在您的计算机中,需要执行哪些步骤?

示例 Hello.js 文件如下:

const fs = require('fs');

function processData(input) {
  const ws = fs.createWriteStream(process.env.OUTPUT_PATH);

  var i, result = "";
  for (i = 0; i < parseInt(input); i++) {
    result += 'Hello - ' + i + '\n';
  }
  ws.write(result + "\n");
  ws.end();
}

process.stdin.resume();
process.stdin.setEncoding("ascii");

_input = "";

process.stdin.on("data", function (input) {
  _input += input;
});

process.stdin.on("end", function () {
  processData(_input);
});

在 macOS Mojave 上,步骤是:

  1. 首选项 > 键盘映射 上,添加键盘快捷键 Ctrl+D 到 运行 其他>发送EOF动作;请注意,这可能会删除与此快捷方式关联的其他操作,例如“调试”(致谢 this answer
  2. Hello.js 文件添加到您的项目并在编辑器中打开它
  3. 进入Modify Run Configuration...对话框(Cmd+Shift+A`,输入“修改...”和 select 它)
    1. 确保 Node interpreter:Working directory:JavaScript file: 设置正确
    2. 如果解决方案将输出写入文件)还将 Environment variables: 设置为 OUTPUT_PATH=./hello.txt 或根据需要设置
    3. 保存
  4. 运行配置。在打开的终端窗格中:
    1. 提供所需的输入;例如4,然后输入
    2. Ctrl+D 触发 "end" 事件
    3. 检查生成的文件hello.txt

您可能想使用console.log()来帮助调试;确保在将您的解决方案提交回 HackerRank 之前将其注释掉。


运行配置:

运行 工具 Window:

试试这个在 windows 上的效果,你可以 运行 它使用这里的节点 当你按下 ctrl+D 时会触发结束事件,就像 hackerrank 或 Mac os


'use strict';
const { METHODS } = require('http');
const readline = require('readline')
process.stdin.resume();
process.stdin.setEncoding('utf-8');
readline.emitKeypressEvents(process.stdin);
let inputString = '';
let currentLine = 0;
process.stdin.setRawMode(false)
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('keypress', (str, key) => {
    if (key && key.ctrl && key.name == 'd'){
        inputString = inputString.trim().split('\n').map(string => {
            return string.trim();
        })
        main();  
    }
});
function readLine() {
    return inputString[currentLine++];
}
function method() {

}
function main() {
    const n = parseInt(readLine().trim());
    const arr = readLine().replace(/\s+$/g, '').split(' ').map(qTemp =>parseInt(qTemp,10))
    method();
}