Javascript Duvida Simples

Asked

Viewed 220 times

-4

Could someone help me in this test I’m not understanding regarding creating a third variable to store the attribute of A = B and B e = A inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • 2

    Your question seems to have some problems and your experience here in Stack Overflow may not be the best because of this. We want you to do well here and get what you want, but for that we need you to do your part. Here are some guidelines that will help you: Stack Overflow Survival Guide in English.

2 answers

0


Hello,

The third variable that says there, is a temporary variable, which keeps the value of A, so that it is not lost when replaced by the value of B.

It’s something like that (Remembering that the code is not complete).

...
var numeroA = 30;
var numeroB = 45;
var temporaria = numberoB;
numeroB = numeroA;
numeroA = temporaria;
...
  • I tried to do in this form tmb, but it returns error: Solution.js:13 var result = numberoB; Referenceerror: numberoB is not defined

  • I was able to solve by reading what you said and what the boy of low tmb quoted thanks worked. ANSWER: var numeroA = 30; var numeroB = 45; var temp = numeroA; numeroA = numeroB; numeroB = temp;

-1

In its solution all variables received the value of the last assignment (numeroA), so numeroB became correct because both she and everything else received the value and numeroA which was 30. In addition to the Marcos dirt, you can also make an array to receive the value of the 2. Thus:

var numeroA = 30;
var numeroB = 45;
var numeros = [numeroA ,numeroB ];
numeros.reverse();
[numeroA ,numeroB ] = numeros;

For you to understand, I passed the value of A and B to numbers so that numbers were like [30,45], then I used the function Reverse to invert the values of the array numbers dai became like this [45,30], then I unstructured the values of numbers in the variables A and B according to the order I put them before numbers, so numeroA receives the value of the first position of numbers and numeroB receives the value of the second position of numbers, more complex but is valid for knowledge.

Browser other questions tagged

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