Skip to main content

Competitive Coding in JavaScript

All over the internet people have debated on this issue, and yet the knowledge about this seems so lost around. This is my first post on Medium, and I was compelled to choose this topic. If you have read some stuff about this before, I suppose everything you read was on the negative. That’s true, however I just believe that everyone has the right to try and then form an opinion instead of just reading something and stopping. If you want to just learn How to program in JS for CP, you can skip over the next section and move to the subsequent one.
There is no need to stop anyone from going into CP with JS. My first search on this landed me on Quora where coders were all against JS for the following reasons:
JavaScript is Slow
JS is slow as compared to C, C++ and Java, which are known as the best languages for CP. Even the “brilliant” and “popular” language Python doesn’t seem to make the cut. There are many codes which get accepted as the correct solution in C/C++ for CP questions, but the same algorithm used to code in Python fails citing [TLE]Time Limit Exceeded as the error. (This has actually happened with one of my friends who knew both C++ and Python). For beginners of CP, if you want to familiarize yourself with all the possible errors and basics of CP, here’s a link:
So there is a high chance that in certain complicated questions, your best algorithm still won’t work in the allotted time while using JS.
JavaScript is not a language used in major Competitions
Yes, the main competitions of CP namely, ACM ICPC host only C++ and Java as their main programming language. So if you’re looking to go to such highly reputed competitions, there’s not a chance you’ll be able to fight the war there when your prized language weapon would be banned!

Having summarised everything on the internet, let’s move on to the next step. What happens when you ignore all of this and actually start coding in JavaScript? Not much of a damage, I must say. In every CP hosting site, there is the option to choose your language before submitting your solution. Almost every site has the option to choose JavaScript. For JavaScript there are two options for you to choose from:
  1. JavaScript Node
  2. JavaScript Rhino
I bet the majority of the JS developers would have experienced a slight mental delight when they would have read “JS Node”. And it sure was the case for me too. Don’t worry though, even if you don’t know anything about Node or Rhino, you still can code in them. Here’s a quick introduction about them:
NodeJS: is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside the browser.
RhinoJS: is a JavaScript engine written fully in Java and managed by the Mozilla Foundation as open source software.
Now here’s my story of starting CP in JS. I obviously knew JS and NodeJS, but wasn’t quite sure of taking input in JS. I knew that in the servers of CP sites, they have an input test cases file which they feed to every program code submitted; but without using any modules or form elements, I didn’t know how to do that. And I think that is the only problem any JS developer faces while starting CP. In fact, this is the only reason I wrote this blog!
There are 2 possibilities: One, if you choose JS Node as your coding language submission. In that case here is what you’re going to do:
  1. Syntax for JavaScript Node
var stdin_input = “”;
process.stdin.on(“data”, function (input) {
stdin_input += input; // Reading input from STDIN
process.stdin.on(“end”, function () {
This code right here, is how you’re going to take input. At the end, you might find this line: 
That will be your function running for the submission. So what you need to do after this is:

function main(input) {
// your code here
Remember, the input variable is one test case input for you. Every input will be taken in as a seperate String. You will have to convert these inputs to numbers or whatever you want using parseInt() and other such functions.
That’s it. You just don’t have to bother about anything else here. Just input the “process” lines above in your CP code and you’re good to go. Of course, you’ll have to provide your own logic for it to work too! :-P
For the output, here is the “simple” line:

process.stdout.write("Hi, " + input + ".\n");

2. Syntax for JavaScript Rhino
This was one language selection for me which was only 50% similar at the first glance since Rhino was only some animal for me at that time. However, when I found out the input procedure in this one, this came up as a love child between JavaScript and Java. Here’s the input code:
var sc = new Scanner(System['in']); // Reading input from STDIN
var my_name = sc.nextLine();
Yup, when you run your program, as you might have probably guessed, you’ll use my_name as the argument since that is the acual input.
function main(my_name){
//your code here
Don’t forget to run your function or use IIFE in your code since this code doesn’t explicitly declare the main function to run. You could use any of the function name for any sub-language choice by the way.
Here, the output is exactly same as Java:
System.out.println("Some output");

Yes, it might look that this is quite different from the usual JavaScript that you’ve been practicing, but after writing these starting lines of code, what you’ll be really doing is only your own code and nothing else. All in all, I feel that if your main language is JavaScript and you’re doing CP for fun and improving your logic building, you should go for it!
Hope this article helped you. 
That's me, signing off!


Popular posts from this blog

Namaste JavaScript Quick Notes

Note:  Akshay Saini's Namaste JavaScript is probably the best course for JavaScript developers out there. These are my personal notes that I made while watching the course; they serve more of as an online quick reference for my understanding and revision, and I hope it benefits anyone reading it too! Everything in JS happens inside an Execution Context. Before a JS code is run, memory is allocated and variables are set as undefined   , and functions are set as their exact code in the scope within the Execution Context. The global execution context hosts all the global variables and function definitions. An Execution Context has 2 components: Memory, that stores variables and functions; and Code, that reads and executes the code. Call Stack maintains the order of execution contexts. Since JS is single threaded and asynchronous, at one point of time, only one function is executed which is at the top of the call stack. For each function, an execution context is created before executi

An introduction to APIs

API is an acronym for Application Programming Interface. Let's start with first defining some basic terms: Browser: These are browsers. To visit any website on the internet, you need a browser. Server: Hmm, this is tough. In simple words, server is a computer. Yes, just like the laptop, or PC at your home. The only difference is that it does not have a screen. Of course, there are other differences in technical specifications, but at its core, the server is just, simply, a computer. That's it. So why is it called a server? Because it serves . When you go to a website like , your computer connects to the internet and gets you your search result. But your computer's internet connection has to get that result from somewhere, right? If the google search result is giving you some answers, the answers have to come from somewhere. What is that place? The answer to that some place is: a server. When you click on the search button on google, or hit enter after typing, &q

Review: Nestjs - Finally a scalable way to build APIs

I have been thinking about this for a long time. There HAS to be a defined way to build APIs in a scalable way.  If you have used Node, Express, etc in your side projects, you might have felt that after a point in development, debugging truly becomes a pain. Sure, enterprise-level API codes are great. But a lot of times, these configurations are too much, and probably not even needed in other projects. To be honest, I haven't seen a lot of Open-Source API codes either to make a judgement on how experienced developers build their APIs. Anyway, I came across an amazing framework recently, and I think if you are coding a complex API, this should be your way to go. Nest.js Nest.js is a framework for building efficient, reliable and scalable server-side applications.  You essentially break your APIs into controllers, services, and modules, which allow you to modularize the smallest of functionalities in your endpoints or the API as a whole. Why is modularizing important? As I have talk