1

Say you want to run / debug a HackerRank solution locally on your WebStorm / IntelliJ Idea IDE for for macOS before submitting it. What are the needed steps, considering node.js is already installed in your machine?

Sample Hello.js file as below:

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);
});
Ricardo
  • 3,696
  • 5
  • 36
  • 50

2 Answers2

3

On macOS Mojave the steps are:

  1. On Preferences > Keymap, add a keyboard shortcut Ctrl+D to run the Other > Send EOF action; beware this may remove other actions associated to this shortcut, such as "Debug" (credits to this answer)
  2. Add the Hello.js file to your project and open it in the editor
  3. Enter Modify Run Configuration... dialog (Cmd+Shift+A`, type "modify ..." and select it)
    1. make sure Node interpreter:, Working directory: and JavaScript file: are properly set
    2. (if the solution writes output to a file) Set also the Environment variables: to OUTPUT_PATH=./hello.txt or as desired
    3. Save it
  4. Run the configuration. In the terminal pane that opens:
    1. provide the needed input; e.g. 4, then Enter
    2. press Ctrl+D to fire the "end" event
    3. check the generated file hello.txt

You may want to use console.log() to help debugging; make sure it's commented out before submitting your solution back to HackerRank.


Run Configuration:

enter image description here

Run Tool Window:

enter image description here

Ricardo
  • 3,696
  • 5
  • 36
  • 50
0

Try this works on windows, you can run it using node here end event gets triggered when you hit ctrl+D just like hackerrank or 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();
}


tarak
  • 11
  • 1