<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: anetasargsyan</title>
    <description>The latest articles on DEV Community by anetasargsyan (@anetasargsyan).</description>
    <link>https://dev.to/anetasargsyan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F926275%2F6e6237f6-07fb-45ca-8f22-d52f79f0d6af.png</url>
      <title>DEV Community: anetasargsyan</title>
      <link>https://dev.to/anetasargsyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/anetasargsyan"/>
    <language>en</language>
    <item>
      <title>Functions in JavaScript</title>
      <dc:creator>anetasargsyan</dc:creator>
      <pubDate>Wed, 09 Nov 2022 20:02:21 +0000</pubDate>
      <link>https://dev.to/anetasargsyan/functions-in-javascript-77i</link>
      <guid>https://dev.to/anetasargsyan/functions-in-javascript-77i</guid>
      <description>&lt;p&gt;Another essential part of programming is function. So…&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a function?
&lt;/h2&gt;

&lt;p&gt;The function is a set of statements that are executed when the function is called.&lt;/p&gt;

&lt;p&gt;Let’s first understand how a function is constructed and what it means to &lt;strong&gt;call&lt;/strong&gt; a function.&lt;/p&gt;

&lt;p&gt;To create a function, we will use a new keyword - &lt;strong&gt;function&lt;/strong&gt;. This will tell that what comes after it is a function. Then we need to &lt;strong&gt;name&lt;/strong&gt; our function. The same rules work here for naming a function, except that you &lt;em&gt;can have multiple functions with the same name&lt;/em&gt;. Just the thing is, the program will consider the &lt;em&gt;last one&lt;/em&gt; only. After the name, we have parenthesis, in which we write &lt;strong&gt;arguments&lt;/strong&gt; for our function(there may be &lt;em&gt;none&lt;/em&gt;). Then we open curly brackets, which identify the &lt;strong&gt;body&lt;/strong&gt; of the function. When the function is called, whatever is in its body gets executed. Now let’s get into details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ExampleWithNoArguments(){
    //do something
}

//calling the function with no arguments
ExampleFunctionWithNoArguments(); 

function ExampleWithMultipleArguments(a, b, c){
    //add a, b and c, and return the value
    var sum = a + b + c;
    if(sum &amp;gt; 0){ 
        return(sum); 
    } else {
        return(-sum);
    }
}

var Sum = ExampleWithMultipleArguments;
//type of Sum will be a function, since we assigned a function to it
//aka you can call "Sum" just the way you call 
ExampleWithMultipleArguments

var sumOfNums = ExampleWithMultipleArguments(-5, -6, -8); 
//type of sumOfNums will be number since function RETURNS number

function ExampleFunction(argument){
    var message = "My argument: " + argument;
    console.log(message);
    return message;
}
ExampleFunction("some random string");

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s consider the last one.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;function&lt;/strong&gt; is the keyword, “ExampleFunction” is the name of our function, what is inside brackets is the &lt;strong&gt;argument&lt;/strong&gt;, and in curly brackets is the &lt;strong&gt;body&lt;/strong&gt;, which will be executed when called. &lt;/p&gt;

&lt;p&gt;In the following line, the function is called with a string argument with a value of &lt;em&gt;“some random string.”&lt;/em&gt; To call a function, we simply write its name, open brackets, and write inside of its arguments if we have any. And that’s it. The body of the function will be executed. &lt;/p&gt;

&lt;p&gt;As one of my beloved teachers once explained: &lt;em&gt;function is like a factory, you give it raw materials, and it works on them and then returns a result to you.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In our case argument is &lt;em&gt;the raw material&lt;/em&gt;. When we call this function, a variable with the name of “&lt;em&gt;argument&lt;/em&gt;” is created, and it takes the value which we give in parenthesis (in our case, &lt;em&gt;“some random string”&lt;/em&gt;). The important thing is this variable is &lt;strong&gt;local&lt;/strong&gt; to the function it belongs to, which means you cannot use this parameter &lt;em&gt;outside of the function’s body&lt;/em&gt;. So, ExampleFunction takes the argument and prints in the console “My argument: “ + “some random string” (as our argument has this value). &lt;/p&gt;

&lt;p&gt;Functions may have &lt;em&gt;many parameters&lt;/em&gt; and may have &lt;em&gt;none&lt;/em&gt; as shown in the example. It is not recommended to have many parameters for a single function to avoid messing things up. Also, they &lt;em&gt;may&lt;/em&gt; and &lt;em&gt;may not&lt;/em&gt; return a value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning value from a function
&lt;/h2&gt;

&lt;p&gt;In JavaScript, functions are variables, which means you can create a variable and assign a function to it. If you do this without calling the function, the variable will take the reference to that function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function ExampleWithMultipleArguments(a, b, c){
    //add a, b and c, and return the value
    var sum = a + b + c;
    if(sum &amp;gt; 0){ 
        return(sum); 
    } else {
        return(-sum);
    }
}

var Sum = ExampleWithMultipleArguments;
//type of Sum will be a function, since we assigned a function to it
//aka you can call "Sum" just the way you call 
ExampleWithMultipleArguments

var sumOfNums = ExampleWithMultipleArguments(-5, -6, -8); 
//type of sumOfNums will be number since function RETURNS number
//output: 19

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if you return something, your variable will take that as a value.&lt;/p&gt;

&lt;p&gt;I keep repeating, “return a value.” Returning a value means giving the result of some actions, as shown in the factory example. You gave arguments (&lt;em&gt;raw material&lt;/em&gt;) to the function (&lt;em&gt;the factory&lt;/em&gt;) and got a value returned (&lt;em&gt;result&lt;/em&gt;). After &lt;strong&gt;returning&lt;/strong&gt; function stops its execution, which means you cannot have &lt;em&gt;more than one return statement&lt;/em&gt; in a function, as &lt;em&gt;only the first one&lt;/em&gt; will give away the result and stop the execution of the function. However, you may say, how come we have two return statements in the example? The answer is inside the if statement, so if we had a negative number, the program would ignore the first brackets and jump to the second one. In the end, &lt;strong&gt;only one&lt;/strong&gt; of those statements will be executed. &lt;/p&gt;

&lt;h2&gt;
  
  
  !Note.
&lt;/h2&gt;

&lt;p&gt;The function's return value is the value of the called function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function inside of a function
&lt;/h2&gt;

&lt;p&gt;Since the function is just a variable that does some operations to get its value, it can be declared inside a function. You can have multiple functions created inside of a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
//takes two arguments, gives their product's absolute value 
//as a result
function ProductOfAbsolutes(x, y){

    //is a function, with argument x, which has the value 
   //of x's absolute
    var absoluteOfNumber = function(a){

        //returns (value) absolute of x
        return Math.abs(a);
    }

    //is a function, with 2 arguments a and b, returns 
//their product
    var product = function(a, b){

        //gives the value of a times b
        return a * b;
    }

    //this is return statement for "ProductOfAbsolutes" function
    //first it calls the function "absolteOfNumber" 
    //with argument x, then with y

    //then puts their outputs as arguments to function "product"
    //returns the product
    return product(absoluteOfNumber(x), absoluteOfNumber(y));
}

console.log(ProductOfAbsolutes(2.5, -5)); // output: 12.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s an example of that. Just remember, what’s declared inside the body, stays in the body(most of the time). (Body is { inside of these }).&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional
&lt;/h2&gt;

&lt;p&gt;Just to give an idea of why we need functions, we can always write things when they need to be executed. This helps A LOT with repetitions. If you need to multiply two numbers, then find the square root of the result, then take the negative of that number and divide by 5 for some reason, and you need to do this more than once, you may find it exhausting to write that over and over again, making it complicated not only for you but for the computer as well. Here come the functions. If you need a neater code, or you have some part that will be repeated multiple times (like playing some sound when a user clicks a button), the functions come in handy. They make your project more understandable, easier to navigate, and in the end, have a beautiful structure. &lt;/p&gt;

&lt;p&gt;Thank you for bearing with me this far! If you have any questions, leave a comment, I will answer as soon as I see.))&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Variables in JavaScript</title>
      <dc:creator>anetasargsyan</dc:creator>
      <pubDate>Wed, 09 Nov 2022 19:32:15 +0000</pubDate>
      <link>https://dev.to/anetasargsyan/variables-in-javascript-n54</link>
      <guid>https://dev.to/anetasargsyan/variables-in-javascript-n54</guid>
      <description>&lt;p&gt;As for creating a program, we need to perform some operations. To perform those operations we need some values. Here come the &lt;em&gt;variables&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are variables?
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Variable&lt;/em&gt; is a thing programming language uses to store some piece of information under a specific name. Variable has a &lt;strong&gt;name&lt;/strong&gt;, by which it may be accessed and which cannot be changed after its declaration, and a &lt;strong&gt;value&lt;/strong&gt; - which can be changed. So basically, by creating a variable we retain some part of memory, saving some value in it, and accessing it through its name.&lt;/p&gt;

&lt;p&gt;This was something general. Now, let’s dig into JavaScript variables. &lt;/p&gt;

&lt;p&gt;To create a variable we use the special keyword &lt;strong&gt;var&lt;/strong&gt;. This indicates, that what comes after this expression will be the &lt;strong&gt;name&lt;/strong&gt; of a newly created variable. So, as I mentioned, there we put the name of the variable. By doing so we already created a variable and gave a name to it. However, you can go ahead and assign a &lt;strong&gt;value&lt;/strong&gt; to it at the time of creation by using the &lt;strong&gt;=&lt;/strong&gt; operator, which is used to assign values to variables. The &lt;strong&gt;=&lt;/strong&gt; operator assigns what comes after it, to what was before it. By using the keyword &lt;strong&gt;var&lt;/strong&gt; you can create multiple variables (and also assign them!). See the code below for a better understanding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
var variableName; // you may stop here as your variable 
                 //has already been declared

var otherVariable = 10; //in this example we assigned 10 to 
                        //"otherVariable", which means that 
                        //otherVariable noe holds the value of 10


var number1 = 1, number2 = 2;

var name1, name2, name3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What to name my variable?
&lt;/h2&gt;

&lt;p&gt;You do have some restrictions when it comes to naming variables. The name of the variable &lt;em&gt;cannot start with a number&lt;/em&gt;, but it can include numbers. You &lt;em&gt;cannot use reserved words&lt;/em&gt;, aka keywords, which are the ones we use to tell the language some specific things(like we used &lt;strong&gt;var&lt;/strong&gt; to tell it to create a variable). Names cannot include spaces, or special characters other than &lt;strong&gt;$&lt;/strong&gt; and &lt;strong&gt;underscore( _ )&lt;/strong&gt; . Also, you cannot have multiple variables with the same name, unless you work in and out of &lt;strong&gt;scopes&lt;/strong&gt;. (Will talk about these later)&lt;/p&gt;

&lt;p&gt;Trust me you absolutely do not need some crazy and funny names for your variables. They must be meaningful. You will start off with small projects, and here, you will most probably be able to remember which variable was used for what reason. Nevertheless, in a bit bigger projects you will start forgetting what was “aaaCuteNameee” used for. So, make it a habit to choose variable names wisely. Take this as friendly advice from me :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Can the values that are given to a variable change?
&lt;/h2&gt;

&lt;p&gt;Absolutely! When you assign a value to a variable, you simply tell the computer, that this name means its value. In our example, &lt;strong&gt;otherVariable&lt;/strong&gt; is 10, so whenever I write the name otherVariable, I mean 10. Any value can be assigned to a variable, anytime, just the same way we did with &lt;strong&gt;otherVariable&lt;/strong&gt;, which is, to write the name, then the operator &lt;strong&gt;=&lt;/strong&gt;, then the value we wish to assign. &lt;/p&gt;

&lt;h2&gt;
  
  
  !Note.
&lt;/h2&gt;

&lt;p&gt;When you have already declared the variable (wrote var name; in your code) you no longer need to declare it before assigning the new value.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some notes on variable declaration in JavaScript
&lt;/h2&gt;

&lt;p&gt;The keyword &lt;strong&gt;var&lt;/strong&gt; is not the only keyword in JavaScript used for initializing variables. There is also &lt;strong&gt;const&lt;/strong&gt; which creates a variable, you can assign a value to it, but you cannot change it afterward. &lt;br&gt;
As you will see in further exploration of the language, there are local and global variables. For now, just remember that there is also the keyword &lt;strong&gt;let&lt;/strong&gt;, which also creates a variable, but in this case local only, while &lt;strong&gt;var&lt;/strong&gt; may as well create a global variable.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Binary representation and operations on binary numbers</title>
      <dc:creator>anetasargsyan</dc:creator>
      <pubDate>Wed, 14 Sep 2022 21:57:20 +0000</pubDate>
      <link>https://dev.to/anetasargsyan/binary-representation-and-operations-on-binary-numbers-29n0</link>
      <guid>https://dev.to/anetasargsyan/binary-representation-and-operations-on-binary-numbers-29n0</guid>
      <description>&lt;h2&gt;
  
  
  What is binary?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Binary is a base-2 numbering system that has two states (“0”, “1”).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why do we use binary?
&lt;/h2&gt;

&lt;p&gt;Computers can understand only 2 states: either there is electricity or there is not =&amp;gt; 0 / 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding binary representation of a decimal number.
&lt;/h2&gt;

&lt;p&gt;The Decimal system is base-10. To represent a number of base-10 system we use our base, and add power to it depending on which row it is on (counting from right to left, starting with 0)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_tJydSax--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlybat3ly5n1k8gqhfmt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_tJydSax--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlybat3ly5n1k8gqhfmt.png" alt="589 represented as decimal" width="880" height="398"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the binary system is base-2, we do the same just changing the “10” by “2”.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zk4Qqz-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxd4vadx57p16sjksvbh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zk4Qqz-w--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uxd4vadx57p16sjksvbh.png" alt="21 represented as binary" width="880" height="366"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(this is 21 in decimal)&lt;/p&gt;

&lt;h2&gt;
  
  
  Conversion from decimal to binary and vice versa
&lt;/h2&gt;

&lt;p&gt;Converting from binary to decimal is what’s shown above: If you calculate the right-hand side, you’ll get 21 which was represented in binary.&lt;br&gt;
Now let’s get to conversion from decimal to binary:&lt;br&gt;
To convert it we need to divide that number by two, and every time write the quotient until you get either 1 or 0. Also, remember to write the remainders right by their side. I’ll give an example of 21. Let’s convert it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R7_EmvNp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s16h0b0a0ay70n83r24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R7_EmvNp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0s16h0b0a0ay70n83r24.png" alt="Binary representation 21 division method" width="512" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we divide 21 by 2, get 10 and 1 as quotient, write it down, and continue. Now we have 10 left: we divide that by 2, get 5, and 0 remainder, write that down and repeat again. Now we have 5: divide that by 2, get 2, and 1 as a remainder. Finally, we divide 2 by two, get 1 for the quotient, and 0 for the remainder. Now we need to “assemble” our binary number. We start from the very end: the last quotient - 1, we write all of the remainders from down-up. So we go in the direction of the purple arrow. I wrote the remainders top-down, so we need to reverse it. Read it down-up, and you will get 10101, which is, in fact, 21 in decimal. &lt;/p&gt;

&lt;h2&gt;
  
  
  Addition of binary numbers
&lt;/h2&gt;

&lt;p&gt;Arithmetic operations we know work for binary numbers as well. They might seem slightly different, but mostly it is the same, just it is a bit unusual for our eyes) Let’s dive into addition.&lt;/p&gt;

&lt;p&gt;To add numbers we can use the traditional method of writing one below the other and add rows with carrying any additionals to the next row. Let’s add 11 to 10 to get 21 again.&lt;/p&gt;

&lt;p&gt;10 represented as the binary number will be - 1010,&lt;br&gt;
And 11 will be - 1011)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--equbIh6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9apsxcboqai6eghxn8wf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--equbIh6B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9apsxcboqai6eghxn8wf.png" alt="Binary addition of 10 + 11" width="880" height="527"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we got 10101, which is, indeed, 21)&lt;/p&gt;

&lt;h2&gt;
  
  
  Multiplication of binary numbers
&lt;/h2&gt;

&lt;p&gt;Multiplication works the same way we are used to: write the numbers we need to multiply one below the other, multiply the rightmost digit of the multiplier with all the digits of the multiplicand. Add a placeholder of '0' before multiplying the next higher order digit of the multiplier with the multiplicand's digits. Repeat the same process for all the following digits. Then add the products to get the outcome of the multiplication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FAISvbL6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8u3wojl41nay6hnjtqc3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FAISvbL6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8u3wojl41nay6hnjtqc3.png" alt="Binary Multiplication" width="880" height="491"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;P.S. I'll do another post featuring subtraction and division of binary numbers :D&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
