Problem solution error [closed]

0
  

Read 2 integer values and store them in variables A and B.   sum of A and B by assigning its result to variable X. Print X   as shown below. Do not present any messages   in addition to what is being specified, and be sure to print the   end of line after the result, otherwise you will receive   "Presentation Error".

     

Entry

     

The entry contains 2 integer values.

     

Output

     

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

In this exercise I put it like this:

var a = 10
var b = 9

var X = a + b

console.log('X = ', X)

And yet you're saying that the resolution of this exercise is wrong. Why?

I'm starting programming.

    
asked by anonymous 12.07.2018 / 17:16

2 answers

1

If you are urionlinejudge even, you may have selected C (gcc) ao instead of JavaScript (up to another language) in the combobox, as shown:

Itshouldprobablybe:

SoIthinkthecodeshouldbethis,becausetheywillpassthevaluestoAandBthroughstdin:

  Notethatautomatedurionlinejudgetestsdonotacceptindex,suchaslines[0]andlines[1],togetthelines,soIused.shift,whichremovesanitemfromthevariableandcopiesittothedesiredvariable,likethis:

varinput=require('fs').readFileSync('/dev/stdin','utf8');varlines=input.split('\n');vara=parseInt(lines.shift());varb=parseInt(lines.shift());varX=a+b;console.log('X=',X);

IncaseitisurionlinejudgeinPortugueseitlookslikeitalreadyrequestsvariableSOMA:

  

Readtwointegervalues,inthiscaseforvariablesAandB.Next,calculatethesumbetweenthemandassignittothevariableSUM.Thenwritethevalueofthisvariable.

Thenchangetothis:

varinput=require('fs').readFileSync('/dev/stdin','utf8');varlines=input.split('\n');vara=parseInt(lines.shift());varb=parseInt(lines.shift());varSOMA=a+b;console.log('SOMA=',SOMA);

Averyimportantdetail,apparentlyyoushouldleavealinebreakattheendofthescript,becausewithoutthisiswhatcausestheerror"Presentation Error" you quoted, according to the message:

  

Do not forget to print the end of line, otherwise you will receive "Presentation Error"

So instead of this:

  

Addthenewemptyline(breakline),likethis:

  

Afteraddingthelinebreakattheendnowitappearedcorrectly:

    
12.07.2018 / 18:18
2

I'll explain.

When you want to join two values, you should use + within the console log between 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 = 'one output, and the other variable X) If it were done this 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 with the space between the second output.

    
12.07.2018 / 17:27