Revisiting JavaScript Essentials

Revisiting JavaScript Essentials

ยท

4 min read

It's been months since I had almost forgotten how I used to solve DSA questions. Earlier like every other college kid, I used to code in C++ or Python on my daily basis. With the passage of time, I got into the field of front-end development, and my love for React and JavaScript has had no limit thus I paused doing competitive coding.

For many days I wanted to take a break from development, as I was pushing myself extremely hard to complete my self-goal of creating 5 full-stack projects using React and AWS. By the way, I have completed my first three full stack apps. I have made "Image Dock" - an image uploader and blog cover image generator. Then I have created a "NotesYard" - a paste bin and note-taking app using react-quill. Then last I have created "Chat Loop" - a chat app built by leveraging GraphQL Subscriptions for real-time communication.

Here are the links for all three you can check them out. Plus these 3 are open source projects, If you like them, feel free to reuse them.

  • Image Dock image.png

Deployed URL: https://image-dock.smilegupta.tech/
Codebase: https://github.com/smilegupta/ImageDock

Coming back to the main topic, so today I was not in the mood to do development so I thought let me open a HackerEarth and let me solve 2-3 competitive coding questions using JavaScript.

Doing competitive coding after a long time and doing competitive coding for the first time are the same in one or the other way.

You will face difficulties in handling the issues like large data type and handling number going beyond the range which JS tries to in fact implicitly convert to infinity ( a type of number used to express large no beyond range). Moreover, you will also face issues in even reading test cases and writing outputs.

One of the most shocking things I found today is, despite the fact in this IT world there are many javascript developers, people still prefer C++ and Python to do DSA and competitive coding.

If you are looking forward to developing good problem-solving skills and improving javascript skills in less time, solving questions on HackerEarth LeetCode, etc can help you totally. Not only you will improve your core-js concepts but also it can become your favorite hobby.

Here are my tips and tricks that will for sure help in kickstarting your javascript coding journey.

First thing first, in any coding platform ( be it be HackerEarth, CodeChef, Leetcode, etc) we get node environment to run our js file and you shall be provided data as an input stream, so you can use node process object to use the input stream of data as below.

process.stdin.on("data", function (data) {
  /* In this function we basically set the value in variables of
  our choice from text case */
  input += data;
});

or you can store the whole chunk of data inside a data structure like an array and then use that data structure for later processing on data, I personally prefer this way.

process.stdin.on("data", function (data) {
  input += data;
  testcaseArray = input.split("\n");
});

In order to output your result, you can use

console.log or process.stdout.write()

Here I have solved 2 basic examples in javascript, I am sure after reading the problem statement and its corresponding solution your all doubts will be cleared.

  • Example 1 (Single Input Problem)

Question: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/find-factorial/

Proposed Solution:

let num = "";
process.stdin.on("data", function (data) {
  num += data;
});

process.stdin.on("end", function () {
  main(num);
});

function main(number) {
  const num = Number(number);
  let ans = 1;
  for (let i = 1; i <= num; i++) {
    ans = ans * i;
  }
  process.stdout.write(String(ans));
}
  • Example 2 (Multiline Input Problem)

Question: https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/vowels-love/

Proposed Solution:

let testcaseArray = [];
let input = "";
process.stdin.on("data", function (data) {
  input += data;
  testcaseArray = input.split("\n");
});

process.stdin.on("end", function () {
  main(testcaseArray);
});
function stringchecker(str) {
  if (
    (str.includes("A") &&
      str.includes("E") &&
      str.includes("I") &&
      str.includes("O") &&
      str.includes("U")) ||
    (str.includes("a") &&
      str.includes("e") &&
      str.includes("i") &&
      str.includes("o") &&
      str.includes("u"))
  )
    return true;
  return false;
}
function main(arr) {
  for (let i = 1; i < arr.length; i++) {
    if (stringchecker(testcaseArray[i])) {
      process.stdout.write("lovely string\n");
    } else {
      process.stdout.write("ugly string\n");
    }
  }
}

I recommend you to start from beginner level if you have not been into much competitive programming and then gradually increase your level and then finally move on to short/long contests.

I wish you all the best for coding, hope this article might have been of some help to you.