Getting Started with Competitive Programming in JavaScript

Getting Started with Competitive Programming in JavaScript

If you are a beginner and want to start doing CP in JavaScript but don't know how to start? You are in right place!

Welcome guys!! to my first ever blog on hashnode as well on the internet. Hope you like the content and up-to your expectations too

Here, you will learn about Competitive Programming using the JavaScript series. In my first blog, I'm going to share about competitive programming, problem-solving stuff using javascript, my learning experience, and other awesome stuff with you.

I have seen there aren't many resources over this. So, I have decided to do make a series on it. Let's get started!!

In this blog, I'm going to guide you on- How to structure a program properly when writing your solution for any competitive programming problem. But first, you need to know: What we need to take input and print outputs in our program. Then, I will give you code two competitive programming problems examples from CodeChef. So, that you can get a better idea about How you can structure your code.

Before that let's discuss some basics in shorts:

Why you should do Competitive Programming in the first place?

You get to solve interesting problems that open your minds and make you a better programmer and problem solver. Believe it or not, but you will benefit from them in your job interviews and in your job finding efficient solutions.

Competitive Programming is not memorizing tons of algorithms

It is training your mind to think faster and efficiently

Why in JavaScript?

So, Why am I doing it in javascript, when there are other more popular languages in the competitive programming world like C++, Java, and Python. I picked JavaScript because I know this language really well and I don't need to spend time learning another language just to be a better problem solver.

If you are a front-end developer who loves Javascript, you should definitely try doing it once!

Here are my tips and tricks that will help you kickstart your competitive programming journey with JavaScript. One thing to know, Is that in any coding platform like CodeChef, codeforces, etc, we get nodejs environment and input is received in an input stream, So to receive that we use nodejs's process object like this:

var input = "";
process.stdin.on("data", (data) => {
    input += data;
})

And to print our result, we can use:

process.stdout.write("your message here");
// OR
console.log("your message here");

Let's understand the structure of code with two examples

Example #1: Number Mirror

Problem link: codechef.com/problems/START01

Here, In this problem we are getting only 1 input and we need to print the same. Let's take a look at the code:

let input= "";
process.stdin.on("data", (data) => {
    input+= data;
})

process.stdin.on("end", () => {
    main(input)
})

const main = (input) => {
    input = num.split('\n')[0]; // We have used split method to convert it into an array by terminating at every \n element.
    process.stdout.write(input);
}

Example #2: Add Two Numbers

Problem Link: codechef.com/problems/FLOW001

Here, the first input will represent the no. of test cases. So, we will have to deal with multiple inputs in this question. Let's take a look at the code:

let input = "";

process.stdin.on("data", (data) => {
    input += data;
});

process.stdin.on("end", () => {
    // converting our input stream into array
    input = input.split("\n");
    let testCases = Number(input[0]);
    for(let i=1; i<=testCases ; i++){
        let number = input[i].split(" ")
        console.log(Number(number[0]) + Number(number[1]));
    }
});

These were two basic examples to get you familiar with the code structure of javascript that we use in competitive programming. Don't get panic if this looks scary to you. As you start solving more questions on these coding platforms you start enjoying them.

I will come back again with more javascript questions to help you in your competitive programming journey. I would really appreciate any feedback or future topic requests from you guys.

Happy Coding!