2

I'm studying to a Championship in Brazil. But I need help because I will do this in JS, but the problem is, they ask to answer with Scanf and Printf. How can I do this in JS? Thanks.

Ps: Its not document.write or something, need Scanf and Printf, if it dont exist in JS, please, let me some link and i will study how works syntax of Scanf and Printf.

2 Answers2

1

First of all...

JavaScript is a general purpose programming language which can be executed in multiple different environments. Each one has it's own way of processing input and output operations.

As you didn't specify which competition is that, I'll assume you're talking about OBI, promoted by UNICAMP (the only one I know that accepts JS solutions and require standard I/O on them). If this is the case, a development environment called Saci is used to run and evaluate JavaScript submissions.

If you want to learn more about writing JS code for the Saci environment, you can search here.

Now, answering your question, two methods are used for standard I/O operations in this environment: scanf and printf. They work like C input/output methods with a few differences.


Writing with printf()

This method work exactly like its C equivalent. You'll see it in the following format:

printf(format, ...);

The first parameter is a string containing the text to be written to stdout. It can optionally contain embedded format tags that will be replaced by the values you specify in the next parameters.

Here's an example:

let x = 34;

printf("%d is an integer\n", x);


Reading with scanf()

This method will work like its C equivalent with two differences: each variable must be specified in quotes, and it'll only work with global variables. You'll find this method in the following format:

scanf(format, ...);

The first parameter is a string containing the format tags used to assign each value to its corresponding variable, which should be informed in the next parameters.

Here's an example:

let x, y;

scanf("%d%d", "x", "y");
sandmann
  • 363
  • 2
  • 13
0

console.log stuff supports the format specifiers a little, but not that useful.

If you are finding one for web browsers, you can use sprintf.js and prompt().

If you are finding one for Node.js:

Community
  • 1
  • 1