Please subscribe to the official Codeforces channel in Telegram via the link https://t.me/codeforces_official. ×

RohitKaushal's blog

By RohitKaushal, history, 2 years ago, In English

javascript : To use in Codeforces

Input
readline() Reads one line from stdin.
to get some space separated values from a line:

var num = readline().split(" ").map(x => parseInt(x)); // num will be an array [1,2,3]
var x = num[0]; // to store values in different variables. 
var y= num[1];
var z= num[2];


var num2= readline(); // reads next line.

Output
print(x); // with auto '\n' (newline)

Refer this submission for a sample program submission.




Node.js : To run your programs locally and submit on CF

readline() do not work by default if you try to use it locally on your computer.

Write this header code in the beginning of your js code

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    
    main();    
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();


function main() {
    const x = readline();
    var line2 = readline(); 
    
    foo(x);
    foo(line2);
}
function foo(x) {
    process.stdout.write("hello: ");   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

Testing Locally

paste your input in a text file input.txt
then
$ cat input.txt | node main.js output on console.
$ cat input.txt | node main.js > output.txt output in file.

you can also make a sublime-build file for this and then just press Ctrl+B
Tools> Build System > new build system > nodejs.sublime-build
{ "shell_cmd": "cat $file_path/input.txt | node $file > $file_path/output.txt" }

Here is a Sample Image :)

 
 
 
 
  • Vote: I like it
  • -11
  • Vote: I do not like it

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

Auto comment: topic has been updated by RohitKaushal (previous revision, new revision, compare).

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

Auto comment: topic has been updated by RohitKaushal (previous revision, new revision, compare).

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

Auto comment: topic has been updated by RohitKaushal (previous revision, new revision, compare).

»
22 months ago, # |
  Vote: I like it +18 Vote: I do not like it

Why so many thumbs down here?

»
20 months ago, # |
Rev. 2   Vote: I like it -20 Vote: I do not like it

why too many downvotes on this post?

»
12 months ago, # |
  Vote: I like it 0 Vote: I do not like it

//node take multiline given number of lines:

const getInput = async (resolve)=>{ const readline = require('readline').createInterface({ input: process.stdin, output: process.stdout }); readline.on('line',(line)=>{ readline.close(); resolve(); }) }

const numberOfInputLines = 10;

for (let i = 0, p = Promise.resolve(); i < numberOfInputLines; i++) { p = p.then(_ => new Promise(resolve => getInput(resolve) )); }

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

You can run javascript code with the V8 JS Engine (it's used once you submit to codeforces) instead of Node to have the readline and print utilities available both locally and once you submit (just like in the first example).

The only issue is that compiling V8 from source takes a lot:D Docs available here: https://v8.dev/docs/build

There's also a feature request proposal at the NodeJS repo for simpler IO processing: see https://github.com/nodejs/node/issues/36184

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Thx finally came to know how input work in js.

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

why is this downvoted? this is actually very helpful

»
6 months ago, # |
Rev. 4   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.

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

how to take input as an array for example, if I have to take the input form n and the user enters the value of array 1 2 3 so ho to take this

  • »
    »
    2 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Input

    1 2 3
    

    Array

    let arr = readline().split(" ").map(x => parseInt(x)); // arr will be an array [1,2,3]
    

    Space seperated variables

    let [a,b,c] = readline().split(" ").map(x => parseInt(x)); // a=1, b=2, c=3
    
    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      thanks, i never solved any question from code forces because I not understand the question but to ask that question in the comment I have to solve at least one question so I use that code in javascript and solved a very simple question