When submitting a solution in C++, please select either C++14 (GCC 6-32) or C++17 (GCC 7-32) as your compiler. ×

atulgairola08's blog

By atulgairola08, history, 4 years ago, In English

All the web developers here who love javascript have always faced some difficulty in using it in competitive programming. Code forces has made vanilla javascript available for competitive programming using V8 compiler of chrome.

To start with javascript v8 in codeforces you need to be able to read inputs and give outputs.

HERE we can use readline() and print(), which reads input and gives output respectively

readline()

This function reads the first line of the input as a string including all the whitespaces.

print()

this prints the provided parameter to the console output.

for ex:

INPUT

2 3 4 5


var x = readline(); print(x);

OUTPUT

2 3 4 5

If you want each number separately, we use split() cause the input is nothing but a string.

So..

INPUT

2 3 4 5


var x = readline().split(' '); // x is an array now [2,3,4,5] print(x);

OUTPUT

2,3,4,5

to take multiline inputs

Mostly in the questions there are more than one lines of input. readline() just reads one line per call.

So, we have to call it again if we need another line input.

INPUT

2

3 4 5


var x = readline(); var y = readline().split(" "); print(x); print(y);

OUTPUT

2

3,4,5

Looping through multiple lines to take input

We usually loop through multiple lines of inputs instead of hardcoding one for each. But we need to be careful that we first declare the var for storing the input outside of the loop else the readline() will keep reading the same line again and again in each loop.

INPUT

3

3 4 5

6 7 8

9 10 11


var x = readline(); // first line of input usually gives the no. of test cases,i.e, the no. of lines ahead. var inp; // declaring the variable outside the loop for(var i = 0 ; i < x ; i++) { inp = readline().split(" "); print(inp); }

OUTPUT

3,4,5

6,7,8

9,10,11

Now you are ready to start your cp journey with js.

  • Vote: I like it
  • +3
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I am getting undefined output afer following code:

var x = readline(); print(x);

What is wrong with above code?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I was fooling around in custom test with javascript v8 and I got the same undefined problem. The thing is that readline() reads a line that ends with '\n'. If yout input has one line, but has no '\n', then readline will return undefined.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Probably necroposting but as long as it helps anyone

I created this library https://www.npmjs.com/package/@ip-algorithmics/codeforces-io

It mocks the readline and print functions and automatically reads from input.txt from the same folder.

Here is a detailed article about how to set up the nodeJs project and how to use it.

https://dev.to/ipreda/codeforces-how-to-use-typescript-javascript-like-a-pro-1cjo

TLDR

npm i @ip-algorithmics/codeforces-io

const { readline, print } = require('@ip-algorithmics/codeforces-io');

Update:

Now you have the option to generate the folder and files with imports with a simple command.

You can find all the details at the end of the article or in the documentation.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    please give me step-by-step setup instructions or provide me any video or something else from which I can understand how to set up this...

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 3   Vote: I like it +1 Vote: I do not like it

      Sure. I'll provide a list of steps. How you can do most of them you can find on google.

      1. Install NodeJs and Npm.

      2. Open a console and type Npm, if an error comes you probably need to add it to the path or restart your pc.

      3. Create a new folder anywhere.

      4. Open a terminal in the folder. Or open the folder in your preferred IDE. For example I use vs code and it has an integrated terminal that opens in the current folder.

      5. Run npm init. Accept default values.

      6. Run npm i @ip-algorithmics/codeforces-io

      7. Create a new js file in the created folder. E.g. problem1.js

      8. Open the file

      9. Add this as the first line const { readline, print } = require('@ip-algorithmics/codeforces-io');
      10. Finish the code for the problem

      11. In the already opened console/integrated terminal just type node problem1.js
      12. You should see in the console the printed result

      13. Copy only your algorithm to the codeforces editor

      14. Enjoy!