Error in troubleshooting

Asked

Viewed 1,333 times

0

Read 2 integer values and store them in variables A and B. Perform the sum of A and B assigning its result to the variable X. Print X as shown below. Do not display any message beyond what is being specified and do not forget to print the line end after result, otherwise you will receive "Presentation Error".

Entree

Input contains 2 integer values.

Exit

Print message "X = " (letter X uppercase) followed by value variable X and end of line. Make sure you have a space before and after the sign of equality, as the example below.

In this exercise I put so:

var a = 10
var b = 9

var X = a + b

console.log('X = ', X)

And yet there you are saying that the resolution of this exercise is wrong. Why?

I’m starting my programming.

  • 1

    I believe you can get [Dit] your question by improving the title. Something like "solution error" doesn’t say much about the real problem and context of your question. Read more on How to choose a good title? Take the opportunity to make the [tour] by the site.

  • Okay. I’ll change it. Thank you.

  • Which website of this exercise?

  • @Guilhermenascimento would say it’s the URI 1003.

  • As for the error, probably because you are not actually reading the values, but rather defining them as constants.

  • @Lucascardoso this is the URI Online Judge exercise itself or picked up from another site?

  • What error does it give ? If it is in the URI, there are several types of errors with their respective descriptions. If it is a "Presentation Error" you will know that it is producing the correct result but formatted wrong. As for javascript in the URI it is used with Node, soon the input comes by a file and is all read as a string through function readFileSync. The template code provided divides by lines with var lines = input.split('\n');. Therefore its first value would come in lines[0] and the second in lines[1] remembering that you have to convert to whole to do what you want.

Show 2 more comments

2 answers

2

I’ll explain.

When you want to join two values, you should use + within the log console between the values, not another comma. The comma passes another parameter to the function, so:

console.log('X = ', X); // output: X =  19 << note que tem dois espaços
console.log('X = ' + X); // output: X = 19 << correto.

This is because instead of concatenating the variable with the string, you passed the variable as a new input to the function, and therefore by default it will be printed with a space between the outputs (it considers the 'X = ' output, and the variable X another) If it were done that way:

console.log('X =', X); //output: X = 19

would work correctly. But in this case, since there was a space at the end of the string, it adds up to the space between the second output.

  • True, I had forgotten this detail. But even so there on the site this giving as not solved this problem. What am I forgetting or misinterpreting?

  • What exercise are you doing, what website? I would need to see to be able to help more, especially if it is a site with automatic correction, it will give you wrong for a simple character in return.

  • What Anderson Carlos Woss said in the comments above is also valid and I had not noticed. The exercise asks you to read two values, not define them. The system will insert the values in a and b, and its algorithm has to add up the values that the system that tests the code der. That is, try to match 'a' and 'b' like this: var a = prompt(); var b = prompt(); Note: I never did Uri in javascript, maybe the way it reads values is different.

  • https://www.urionlinejudge.com.br/judge/pt/problems/view/1001 I’m picking up from this site that I was referred to for beginners, but I don’t know why the problem is

  • Lucas, I did the javascript test there, following: To read the values, you will need to do a little complicated code, so I’ll explain the basics: You need to match 'a' and 'b' to this: parseint(Lines.shift()); Explaining: javascript does not have an exact command to read information on the console, as C has for example scanf(). Did you notice that at the beginning of the javascript code there are two lines of code? That’s for him to read. Lines is the object that will receive the data. Shift will pass the values, viewed raw. and parseint transforms the string into number.

1


If it is the urionlinejudge even, maybe you have selected C (gcc) instead of Javascript (up to another language) in the combobox, as per image:

c (gcc) urionlinejudge

Probably should be:

javascript urionlinejudge

So I think the code should be this, because they who will pass the values to A and B through stdin:

Notes that urionlinejudge automated tests do not accept index, such as lines[0] and lines[1] to pick up the lines, so I used .shift, that removes an item from the variable and copies it to the desired variable, thus:

var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var a = parseInt(lines.shift());
var b = parseInt(lines.shift());

var X = a + b;

console.log('X = ', X);

In case it is the urionlinejudge in Portuguese already seems to ask variable SUMMING UP:

Read two integer values, in the case of variables A and B. Next, calculate the sum between them and assign to the variable SOMA. Then write the value of this variable.

Then change to this:

var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

var a = parseInt(lines.shift());
var b = parseInt(lines.shift());

var SOMA = a + b;

console.log('SOMA = ', SOMA);

A very important detail, apparently you should leave a line break at the end of the script, because without this is what causes the error "Presentation Error" that you quoted, according to the message:

don’t Forget to print the end of line, otherwise you will receive "Presentation Error"

So instead of this:

sem quebra de linha no final do script

Add the new empty line (line break) like this:

com quebra de linha no final do script


After adding the line break at the end now appeared correctly:

resposta correta no urionlinejudge

  • "Note that urionlinejudge automated tests do not accept index, such as Lines[0] and Lines[1]" - but accept. I’ve done a lot of it that way, now there are particular cases where it’s wrong. But I suspect it has to do with how the incoming file is encoded, or any other detail that I didn’t contemplate.

  • console.log always breaks at the end. In such cases the error that gives is even Wrong answer 100%, but it is worth remembering that they have there JS as beta still, so I do not know what implications this may have in the tests.

  • I’ve caught one or the other. Now I can’t remember which ones, but I’m already trying to find and send.

  • @Isac closed, I’ll have to leave, but I promise to see you as soon as possible today

Browser other questions tagged

You are not signed in. Login or sign up in order to post.