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:
JavaScript
x
26
26
1
const fs = require('fs');
2
3
function processData(input) {
4
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
5
6
var i, result = "";
7
for (i = 0; i < parseInt(input); i++) {
8
result += 'Hello - ' + i + 'n';
9
}
10
ws.write(result + "n");
11
ws.end();
12
}
13
14
process.stdin.resume();
15
process.stdin.setEncoding("ascii");
16
17
_input = "";
18
19
process.stdin.on("data", function (input) {
20
_input += input;
21
});
22
23
process.stdin.on("end", function () {
24
processData(_input);
25
});
26
Advertisement
Answer
On macOS Mojave the steps are:
- 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) - Add the
Hello.js
file 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.txt
or 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: