HackerRank JS Boilerplate

Summary

I put together this boilerplate to let me run through the exercises more easily with a simple CLI command.

Details

If you're solving problems with JavaScript in HackerRank, you can simulate their environment locally by installing node.js and passing this JS boilerplate along with the sample input as shown below.

I use this setup in emacs to quickly iterate over problems.

Setup

I use chocolatey to quickly setup the environment on my windows based laptop.

C:\> choco install nodejs.install

I'm using node 0.12.7 but I imagine this will work with more recent versions.

Edit (2018-05-25): I would recommend using nvm (Node Version Manager) to manage node efficiently and install a recent stable version.

C:\> choco install nvm

JavaScript boilerplate

This will read in the problem statement input from a text file.

/* ******************************************************************************
 * Boilerplate for HackerRank
 * ******************************************************************************/
var readline = require('readline');
(function() {

    var inputLineArray = [];
    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.on('line', function(input) {
        inputLineArray.push(input);
    });

    rl.on('pause', function() {
        solveProblem(inputLineArray);
    });
})();

Full example

I'm using the warmup "staircase" problem to illustrate.

Input data

(staircase.txt)

6

Problem solution

(staircase.js)

/* ******************************************************************************
 * Problem Solution
 * ******************************************************************************/
function solveProblem(input) {
    var data = prepareInput(input);
    buildStairCase(data.size);
}

function buildStairCase(size) {
    var stair = '';
    for (var i = size - 1; i >= 0; i--) {
        console.log(buildStair(i, size));
    }
}

function buildStair(level, size) {
    var stair = '';
    var paint = '';
    for (var i = 0; i < size; i++) {
        paint = setPaint(i, level);
        stair += paint;
    }
    return stair;
}

function setPaint(index, level) {
    var paintOff = ' ';
    var paintOn = '#';
    if (index < level) {
        return paintOff;
    } else {
        return paintOn;
    }
}

function prepareInput(input) {
    var size = input.shift();
    return {
        size: parseInt(size, 10),
        input: input
    };
}


/* ******************************************************************************
 * Boilerplate for HackerRank
 * ******************************************************************************/
var readline = require('readline');
(function() {

    var inputLineArray = [];
    var rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout
    });

    rl.on('line', function(input) {
        inputLineArray.push(input);
    });

    rl.on('pause', function() {
        solveProblem(inputLineArray);
    });
})();

Usage

In the folder where your JS solution file is located:

C:\> node staircase < staircase.txt

Output

     #
    ##
   ###
  ####
 #####
######

Emacs screenshot

Emacs Screenshot showing hackerrank boilerplate and node.js output