'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);
});
Solution 1:[1]
On macOS Mojave the steps are:
- On Preferences > Keymap, add a keyboard shortcut
Ctrl+Dto run the Other > Send EOF action; beware this may remove other actions associated to this shortcut, such as "Debug" (credits to this answer) - Add the
Hello.jsfile to your project and open it in the editor - Enter
Modify Run Configuration...dialog (Cmd+Shift+A`, type "modify ..." and select it)- make sure
Node interpreter:,Working directory:andJavaScript file:are properly set - (if the solution writes output to a file) Set also the
Environment variables:toOUTPUT_PATH=./hello.txtor as desired - Save it
- make sure
- Run the configuration. In the terminal pane that opens:
- provide the needed input; e.g. 4, then Enter
- press Ctrl+D to fire the
"end"event - 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:
Run Tool Window:
Solution 2:[2]
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();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | tarak |


