Take input in NodeJS [ For all Online Judges ]

Introduction

When solving coding problems on online judges, one of the essential tasks is to read input from the standard input stream (stdin). In some programming languages, such as Python and Java, reading input from stdin is relatively straightforward. However, in Node.js, it can be challenging and cumbersome, especially for beginners.

In this tutorial, we’ll show how read input from stdin in Node.js, and we’ll provide a sample code that you can use as a template for your coding problems.

I’ve seen many template code for taking input in NodeJS. But all of them are not useable by beginners.

Reading input from stdin in Node.js

Use this template written by me:

let _inputData="",_inputArray=[],_count=-1;function input(){let n=_inputArray[_count];return _count++,n}process.stdin.setEncoding("utf8"),process.stdin.on("data",(function(n){_inputData+=n})),process.stdin.on("end",(function(){_inputArray=_inputData.split("\n"),input(),main()}));

// You need to code in this main function.
function main() {

  let a = input(); 
  a = parseInt(a);
  console.log(a);
}

Here, to read a line of input, just call the function input().
input() function will return a string. You can simply apply NodeJS method like parseInt() to convert it to int

For each call, input() function will return one line of the stdin. If there’s no more input to read, it’ll return undefined

I’ve minimized this code so it doesn’t look like a mess.
If you want to see the commented long code:

Also, I’ve solved some problems using this template in toph.co
You can see those if you’re still confused on how to use this template. :slight_smile:
My NodeJS submissions: Submissions | Toph

Use the code snippets provided in this tutorial as a starting point for your own solutions, and happy coding!

Author:

Nusab Taha

6 Likes

Nice work kid.
Keep it up

1 Like

Thanks man. :slight_smile:

@Nusab19 good. But maybe this can be solved in a better way? People shouldn’t write so much code to read a line after all when they are here to solve problems not implement I/O. Codeforces solves it in a better way, the provide a built in function readline(), which from reading their post is supported by the engine they are using by default??? (Or something like that?)

Here in this post Mike writes:

With the help of a tambourine and a liter of cola I successfully compiled it on Windows. Funny, I was ready to implement workaround to support reading from stdin in JavaScript, but d8 already supports it! Just use readline() to read line from the input.

So that means even without d8 (or whatever) having default support for it cf already had plans to implement something like readline() manually.

@hjr265, maybe consider resolving this issue of having to manually implement I/O in JS by using whatever implementation for JS Codeforces is already using?

I do believe that this is the best way ahead as I am again I am supporter of Solve the problem not the language - motto (which does go against common bd c/c++ supremacy belief i guess and the common bd belief that compiled languages are always better etc.), and hate things like presentation errors and fast I/O stuff and so on.

So, basically again I will be saying that

People shouldn’t write so much code to read a line after all when they are here to solve problems

And I/O features for JS should be provided by the OJ since it does not look like a non-existent solution after all.

2 Likes

Yeah. I just saw CodeForces’s approch. It’s great and simple.

Toph could implement something similar or even better. However, some online judges have their own input/output methods for NodeJS, while others do not.

Also there are online NodeJS compilers that don’t support I/O.
So, this template is kind of line an Universal way of taking input in NodeJS. Whether that platform supports readline() or not, this template will always work ( hopefully ).

Personally, I prefer the curly braces syntax of C++ but also appreciate the simplicity of Python. That makes NodeJS a great choice for me. But sadly, Toph didn’t support any built in method for I/O.
Therefore, I left solving problems in NodeJS.

It’d be great if Toph take some steps regarding this.

Also, @Nusab19 to clarify, I actually like your post on NodeJS I/O. However, I have several problems with this. Not exactly with your post but the way JS has been implemented on Toph as general.

To begin with, I do not think that NodeJS is a good choice for online judges in the first place. Surely, Node adds a lot of utilities and features to assist development but I don’t think any of those are adding any value to problem solving. Except you need fs for I/O. But I do not think it is the way an OJ should take (for the reasons mentioned on the previous post). So, V8 feels like a better solution for OJs to me.

I actually have thought about solving problems on Toph with JS to increase my familiarity with JS on many occasions when I was a JavaScript beginner but didn’t like how it works on Toph and how I had to write I/O methods myself when I was here to just write more JS code to solve problems and get familiar with the syntax and data types and common functions and so on. It felt like a chore and I ended ditching the idea whole together and forgot about it over time.

Thanks to your post I can finally share those thoughts with others now.

But the V8 implementation that CF used is also a engine of NodeJS.

I don’t have much familiarities with JS. But doesn’t pure JS have many things like DOM and etc ?
I don’t think JavaScript is needed in Toph. Rather the V8 would be better. Because it just parses and runs JavaScript.

Still, I’m not so familiar with JS. So I might be wrong.

No, v8 is simply a JavaScript implementation and NodeJS is v8 + features.

But I simply think that except I/O none of those features are needed for problem solving anyways. So, if Codeforces uses d8 which has readline() by default it would be for the best.

What would you do other features of NodeJS anyways? Generate cryptographically secure random bytes with crypto or spawn process with child process? Those are not something meant to assist problem solving.

Note that I do like NodeJS a lot and pretty sure most people who did any JS development also does. However, I simply think Node features are not a necessity for problem solving. That’s all.

1 Like

Thank you @Nusab19 for this post. It is really useful.

I think it also makes sense to add D8 as another language option under the JavaScript group. I will look into it in sha Allah. Thank you @touhidurrr for suggesting this.

2 Likes

Toph now supports d8 as a programming language, experimentally.

1 Like

Wow!
I just wonder how quickly you added D8.

Thanks a lot for this! <3

1 Like

I can show you a little summary of the commit that got the feature in:

image

This is Go code. Out of the 15 lines added, 4 of them are } and 1 one of them is an empty line. :slightly_smiling_face:

1 Like

haha. You made it seem too easy :3

I have another feature request. :slight_smile:
In addition to being able to give stars to someone’s post, it would be very useful to have the option to react with emojis. This is not a high-priority request, but it would make it easier to express emotions using emojis.

1 Like

Just tried it @hjr265 @Nusab19 and it does look like quite fun.
Coding in d8 feels like Python but more fun as JS syntax is more free.
Here is a submission that I made:

const [a, b] = readline().split(' ').map(e => +e);
console.log(a + b);
1 Like

However it looks like the editor is not remembering my last selected language @hjr265. Might be a bug.

Also, it looks like all inputs should end with a newline character. readline() fails for certain problems such as:

1 Like

That, unfortunately, is a limitation of readline in d8.

But at the same time we want to have LFs for each line in test case inputs and outputs. If you think a problem is lacking that, please raise it in the corresponding discussion topic for that problem.

@Nusab19 Done

1 Like

https://community.toph.co/t/many-problems-inputs-not-ending-with-a-newline/

1 Like

In, https://toph.co/settings/problem_archive
There’s an option to select a default language.
It works only for file submission, not for the editor.

During contest, it becomes a bit annoying to scroll and select the language each time. Might be a bug.

p.s: Thanks a lot for the reaction update! It’s great!