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:
https://hackerearth.com/practice/codemonk
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
process.stdin.resume();
process.stdin.setEncoding(“utf-8”);
var stdin_input = “”;
process.stdin.on(“data”, function (input) {
stdin_input += input; // Reading input from STDIN
});
process.stdin.on(“end”, function () {
main(stdin_input);
});
This code right here, is how you’re going to take input. At the end, you might find this line: 
main(stdin_input);
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:
importPackage(java.io);
importPackage(java.lang);
importPackage(java.math);
importPackage(java.util);
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!

Comments

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

i3wm essentials - I (Brightness)

So you have started using i3 and somehow managed to open your browser and almost resumed your normal work.  But wait, the brightness is too much isn't it? Or is it too low? The mousepad used to work fine, but now all of a sudden tapping does not equal click?!  Don't worry.  This blog series will tell you all about the essential setup commands and common shortcuts that I use to navigate my work in i3, and how you can too. Changing the brightness So you just started i3 and you just can't take this brightness setting. You go for your function keys, and damn! They aren't working. Quick fix: Run the following command if you need to change the brightness ASAP. xrandr -q | grep ' connected' | head -n 1 | cut -d ' ' -f1 This will give an ouput that's the name of your monitor.  Use that monitor name here and change the values of brightness to suit your needs. xrandr --output <monitor-name> --brightness 0.7 Now that your eyes are comfortable, let me show

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 google.com , 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