Wednesday, May 18, 2022

This piece of Js code explained

 I got this piece in the mozilla.

// Statement1 : The elephant weight less than the mouse

What is write in the line after -->// is something that isn't run in a Js code doesn't work for HTML or CSS, and also jump for the next line if you are still commenting will gonna have to put in the beggining of the comment again.

So what I am proposing is something that I gonna test if true or false.

const eleWeight = 1000;

const mouseWeight = 2;

Here in this lines I create the variables that will be my comparision paremeter, I gonna compare the two values to think about the sentece ->  Statement1 : The elephant weight less than the mouse, so I called the eleWeight the elephant weight and the mouse of mouseWeight, and I gonna compare the two and the sentence said that an elephant 1000 is less weight than mouse 2, so is said that 1000 < 2, so I pretend receive an answer true or false, and you see that is false. But in along the code  I gonna make be printed isntead just true or false an sentence if true that the elephant weight less and if false this isn't true the elephant weight more.

So I gonna generate more one const that will say mathematically what is in the comment:

const weightComparison = eleWeight < mouseWeight;

If I put this way in the console I gonna receive an answer -> false

const section = querySelector('section');

Here I am saying that already have an bject section in my HTML and I am justing attaching an variable to an object in the HTML to I could manipulated in the Js so what have in the HTML have to be create an variable to could be used in the Js you can't just jump for the section, I have to make it an variable in the Js to could use it. 

Section.innerHTML = '  '; 

 This way I could have write in the HTML and not in the Js, this text will not suffer changes, I am not seeing no action reffered to the text, you just choice write that space (brrrrreee) in Js and not in HTML, is very unecessary put an space sometimes an space is need because we need that the section be dimensional, so I write any shit on it to be dimesnional or will gonna be admensional is like exists but doesn't ocuppy place in the space.

 const para1 = document.createElement('p');

Now I create another variable called para1, this help knew that para is for paragraph and 1 because the original was 3 paragraph for 3 statements, I am just doing the first.

document. createElement is because I am creating an element that doesn't exists in the HTML so I create the paragraph in the Js and if everything work will create an paragraph in the HTML that is the document, and what is -->('p')?, is that what I want to create will be in parenthesys, and 'p' is what is gonna be create in the HTML, in the HTML 'p' is paragraph. I am creating in Js an paragraph in the HTML, isn't the only way because I could have already the paragraph create in the HTML and just find the paragraph, the only excuse for that behavior oh oh oh, is that if I have 3 paragraph how diference them in the Js I would have to give a different class or id for each, more work so let this way but only one paragraph I could put in the HTML.

const weightTest = weightComparison ? 'True - elephant weight less than mice!? ' : 'False - of course an elephant is heavier than a mouse! ' ;

Now I create an variable to hold the result but I could write the answer directly inside the paragraph, but here was prefer hold the answer in an variable, everytime I want the answer I just to put the variable or otherwise I would have to write all the function again, so an value is something and function is other, is like an function the test will be run again, and an variable an answer are given without am calculation, is like this if I have to use an function for some values 300 times I gonna have 300 times re-calculted when I could print the same answer 300 without effort for the machine, maybe an faster work in any place.

And what is this function is like this --> 

[what I am comparing to have an boolean value(true or false)] ? 'if true display this' : 'if false display this';

So the comand is -->      TEST ? 'IF TRUE'  'IF FALSE';

So instead of have print true I said if the sentece is true print this, and isntead of have false I said replace false by this sentence. 

para1.textContent = weightTest;

So now I am placing something in my para1, isn't enought make the test I wanted to be write in my HTML, so inside the paragraph will be the result in the weightComparision I am comparing two values with the mathetical symbol of '<' but the answer of the comparison is inside the variable weightTest, so the weighTest is what I print because I want the result, and the result is only in the last variable, I used 2 variables and the one that holds the result is the last one is because people got troubled to knew why one variable is unconsidered is because I used 2 variable and the result is in the last the other only hold the comparision, the 1st is the statement and not the conclusion about it, is more elegant I make the afirmation that I want the answer and after produce the answer, instead of make the afirmation and inside the own statement the answer.

Let look the structure is para1 dot the action and after the ' = ' , the value of the action;

section.appendChild(para1);

 And here until that moment I create an paragraph and everything but untill that moment I haven't said where in the HTML the paragraph is, so here I am attaching the paragraph to the section, I could have make of a diferent way and have the same result, if I had create the paragraph in the HTML and just make an variable for it in the Js this step would be not necessary, but the paragraph was create in the Js so I have to say where gonna be the paragraph so the section that was create in the HTML is there only to I said that all the paragraphs will be append to the section, see the section is an empty space that exists in the HTML to append the paragraphs, so the comand is section.appendChild(para1);

Rute Bezerra de Menezes Gondim


No comments:

Post a Comment