Wednesday, July 30, 2014

My first html/javascript code on "functions"

  My first html/javascript code on functions. 

 @ first I thought javascript would be java and nothing more. But, today, I can say YES it is and NO it’s not. Yes because, if you understand java then you ‘re almost done with javascript. No because, you still got some catching up to do. See the below code. In java, you dare not call a no argument(s) method with argument(s).
 More so, everything (variables, data types, etc…) you use is either implicitly defined which you‘ve to import to your code ""except java.lang class"" OR explicitly defined by you.
 Well, I believe every language has it own style. You just have to understand every single logic.
---------------------------------------------------------
Line 8 declared a function add with no argument. Line 17 calls the add functions with arguments… Even though i clearly understands how it works, it still looks awful to me compare to java.
 What the below code actually does is – create a function call add(), line 8. Declare a variable named value which is initialized to 0, line 9. Create a for loop that add/sum the passed in parameters, line 11 – 13. Return the sum value, line 14. Line 17 shows the passes in values/parameters (2,4,6,8,10) while line 18 displays the value (sum).
---------------------------------------------------------

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title> JavaScript Functions </title>
5.
6.   <script type="text/javascript">
7. 
8.    function add() {
9.     var value = 0
10.       
11.        for(i=0; i < arguments.length; i++) {
12.        value+=arguments[i];
13.        }
14.     return value;
15.    }
16.   
17.    var mainValue = add(2,4,6,8,10);
18.    alert(mainValue);  
19.
20.   </script>
21.
22.
23. </head>
24. <body>
25. <h1> JavaScript Functions  </h1>
26. </body>
27. </html>
-----------------------------------------------------------
RESULT = 30
-----------------------------------------------------------

--------------------------------------------
LITERAL FUNCTION – create a function without a name then assign it to a named variable Ooops. @ the end of the function declaration, include the function parameters (line 10)
--------------------------------------------

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title> JavaScript Function </title>
5.
6.   <script type="text/javascript">
7.  
8.   var Ooops = (function(){
9.     return arguments[1] + arguments[2];
10.   })(1,2,3);
11.
12.    alert(Ooops);
13.
14.   </script>
15.
16. </head>
17. <body>
18. <h1> JavaScript Function </h1>
19. </body>
20. </html>
-----------------------------------------------------------
RESULT = 5
-----------------------------------------------------------

--------------------------------------------
ANONYMOUS FUNCTION – create a function without a name. @ the end of the function declaration, include the function parameters (line 10)
--------------------------------------------

1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title> JavaScript Anonymous Function </title>
5.
6.   <script type="text/javascript">
7.  
8.   alert((function(){
9.     return arguments[0] + arguments[1];
10.   })(4,5,3));
11.
12.   </script>
13.
14. </head>
15. <body>
16. <h1> JavaScript Anonymous Function </h1>
17. </body>
18. </html>
-----------------------------------------------------------
RESULT = 9
-----------------------------------------------------------
KNOW THIS – if you ‘re already familiar with java & new to javascript THEN your new friend will be SYNTAX ERROR(S)