Skip to content
Advertisement

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

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);
});

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement