Learn JavaScript 04 (functions)

Hey, How are you? ūüôā I think u r getting well. Ok first look at the below code.

Capture 10

Just type and run. You can’t copy and paste ūüėÄ Type your¬†own code and run. It is the best way to practice ūüôā
Ok did you get any idea what we are gonna talk about. Yes we are going to talk about JavaScript FUNCTIONS.

In JavaScript we use function keyword to define a function, followed by a name, followed by parentheses ().

Syntax
function function_name(parameter1, parameter2, parameter3) {
code to be executed
}

Capture 11

Let’s consider about this small program. In here function name is “myFunction” and “a” and “b” are the parameters. This function returns the multiplication of a and b.¬†When JavaScript reaches a return statement, the function will stop executing.
In “myFunction(4,3)” ¬†we call the function and pass value 4 to letter a and pass value 3 to letter b.

Nested Functions

Capture 12
Did you get that ? ¬†The “square” function is in the “myFunction” function. Oh! Function function everywhere ūüôā ¬†don’t get confused just think simple. Nested Functions mean function within a function. This is a simple definition for that. We can learn more about future parts.

Function() CONSTRUCTOR

The above way is not the only way to define a new function. We can define function dynamically using Function() constructor along with the new operator.
Now you may think what is constructor.
“A constructor is a special method of a class or structure in object-oriented programming that initializes an object of that type. A constructor is an instance method that usually has the same name as the class, and can be used to set the values of the members of an object, either to default or to user-defined values”

For example, if you have a Car class, you can create new objects of this type by saying new Car() . We can understand about constructors by looking some examples.

Syntax


var variablename = new Function(Arg1, Arg2, “Function Body”);

The Function() constructor can hold any number of string arguments. The last argument is the body of the function. It can contain arbitrary JavaScript statements, separated from each other by semicolons.
Youknow? the Function() constructor is not passed any argument that specifies a name for the function it creates. The unnamed functions created with the Function() constructor are called anonymous functions.

Capture 13
This is a small program which explains what constructor function is. You can see the underlined statement. It is the line which create a “funct” function. “x” and “y” are the parameters and “return x*y;” is the function body. Ok ¬†I think that you got the idea.

Ok boss ūüôā This is the end of Function part. We can get more idea about functions in future parts.

 

 

Learn JavaScript 03

In this part let’s talk about some important things like syntax, variables, ….etc.

  • JavaScript uses the var keyword to declare variables.var x;
    x = 10;

     

  • JavaScript uses an assignment operator ( = ) to assign values to variables.var x = 4;
    var y = 10;

     

  • JavaScript uses arithmetic operators ( + – *¬† / ) to compute values.(2 + 3) * 6;

     

  • JavaScript allows you to work with three primitive data types.Numbers: 12, 12.50
    Strings: “Hello Sam”
    Boolean: true or false.
  • JavaScript comments.¬†Code after double slashes // or between /* and */ is treated as a comment.// This is a single line comment.

    /*
    This is a multi line
    comment in JavaScript
    */

  • JavaScript variables must be identified with unique names.
    These unique names are called identifiers.
    JavaScript identifiers. In JavaScript, the first character must be a letter, an underscore (_), or a dollar sign ($). Subsequent characters may be letters, digits, underscores, or dollar signs.

    Numbers are not allowed as the first character.
    JavaScript identifiers are case sensitive.

  • JavaScript Arithmetic Operators
    Operator Description
    + Addition
    Subtraction
    * Multiplication
    / Division
    % Modulus
    ++ Increment
    Decrement
  • JavaScript Assignment operators.
    Operator Example Same As
    = x = y x = y
    += x += y x = x + y
    -= x -= y x = x – y
    *= x *= y x = x * y
    /= x /= y x = x / y
    %= x %= y x = x % y

Learn JavaScript 02

In first part we discussed about some facts about JS. Let’s go ahead….

Here let’s talk about OUTPUTS in JavaScript.
JavaScript does NOT have any built-in print or display functions. JavaScript can display data in different ways.

  • Writing into an alert box, using window.alert()

    Capture 4-alert
    try this code and you can see  an alert box.

  • Writing into the HTML output using document.write().

    Capture 5
    The document.write() method should be used only for testing. Because If we’ll ¬†use document.write() after an HTML document is fully loaded, will delete all existing HTML.
    below example will explain it. Try this code and you can understand what I’ve told above.

    Capture 6

  • Writing into an HTML element, using innerHTML.

    By using document.getElementById(id) method, we can access an HTML element.
    The id attribute defines the HTML element. The innerHTML property defines the HTML content:

    Capture 7.1
    When we click the “click” button, document.getElementById() method runs and It passes the “Ohh this is cool” string to header which id = “ID”.

  • Writing into the browser console, using console.log().

    In browser we can use the console.log() method to display data. For this we have to activate the browser console with F12 and select console in the menue.

    Capture 8
    First run the html file. Then press F12 and you can see a window. then click a console menue and you can see the answer 11.

OK in this part we discussed about OUTPUTS in JavaScript. In the next part
let’s discuss about more things.

 

 

 

 

Learn JavaScript 01

JavaScript is the programming language of HTML and the web. It is a dynamic computer programming language. We use JavaScript to program the behavior of web pages. It is a interpreted programming language.

Let’s look a small example…..

Capture 1

above code gives you this

Capture 2

by clicking the Click Me button you can change the content

Capture 3

  • Ok! now we have small idea that what JS(JavaScript) can do. Now we are going to learn JS.JavaScript can be implemented using JavaScript statements that are placed within the HTML tags in a web page.
         JavaScript codeJS tag takes two important attributes.
  • Language:¬†This attribute specifies what scripting language you are using.
  • Type:¬†This attribute is what is now recommended to indicate the scripting language ¬† ¬† ¬† ¬† ¬† ¬† ¬† ¬†in use
         JavaScript codeWe can be placed these tags in the <body> and the <head> sections of an HTML page.

Ok as usually let’s start from HELLO World ! ¬†ūüôā

<html>
<body>

¬† ¬†document.write (“Hello World!”)
</body>
</html>

This code will produce the following result:
Hello World!

We can talk more about JS by next parts.

Java SERVLET 01

What is a servlet ?

  • Servlet is a technology that is used to create web application.
  • Servlet is an API that provides many interfaces and classes including documentations.
  • Java Servlets are programs that run on a web server and used for developing web applications.
    we can understand about servlet by this example. Let’s consider where we need to log on to a web site to access our e-mail account. We have to submit username and password for authentication. Then web site accept the login information and validates it using server side programs. (like servlets)
Capture
client -> server -> response -> client

 

  1. request
  2. response is generated at runtime.
  3. response is sent to the client.

JSP

OK let’s see what is JSP(java servlet pages)

Java Server Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>

Simply we can understand it by this…..


Capture1.JPG

This is a simple jsp program. We save this as  filename.jsp
JSP is used for creating dynamic webpages. Dynamic webpages can have two types of contents ‚Äď static & dynamic content.¬†The static contents¬†can have text-based formats such as HTML, ¬†XML etc. and the dynamic contents are generated by JSP elements.

I know u have no clear idea about jsp yet ūüôā ok first let’s look about
jsp processing.

  • your browser sends an HTTP request to the web server.
  • The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine.
  • The JSP engine loads the JSP page from disk and converts it into a servlet content.(all template text is converted to println( ) statements and all JSP elements are converted to Java code that implements the corresponding dynamic behavior of the page.)
  • The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.
  • The servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format, which the servlet engine passes to the web server inside an HTTP response.
  • The web server forwards the HTTP response to your browser in terms of static HTML content.
  • Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page.

 

OK now we know some facts about jsp servlet. (actually nothing ūüėÄ ) next part we will know something more………………..
LEARN SOMETHING

PHP Qs

  1. What are the correct statements about Cookies in PHP ?

a)A cookie is often used to identify a user.
b)A cookie is a small file that the server embeds on the user’s computer.
c)Each time the same computer requests a page with a browser, it
will send the cookie too.
d)With PHP, you can both create and retrieve cookie values.

2.  A cookie is created with the ____________ function.

a)setcookie()
b)SetCookie()
c)setCookie()
d)set_Cookie()

3.  setcookie(name, value, expire, path, domain, secure, httponly);

Only the name parameter is required. All other parameters are optional.
Is this statement correct?

a)TRUE
b)FALSE

4. What are the correct statements ?

a) The setcookie() function must appear BEFORE the <html> tag.
b) The value of the cookie is automatically URLencoded when sending the          cookie, and automatically decoded when received
c) If want To modify a cookie, just set the cookie using the
setcookie() function.
d) To delete a cookie, use the setcookie() function with an expiration date in      the past.

5. The………………….function returns a string with backslashes in

front of the specified characters. 

a) Addcslashes()
b) addcslashes()
c) add_slash()
d) add_backslash()

6. Select the correct statement(s)

a) addcslashes() function returns a string with backslashes in front of the       specified characters.
b) addcslashes() function is not case sensitive.
c)
<?php
$str = addcslashes(“Hello World!”,”W”);
echo($str);
?>
output of this code is     Hello \World!

d) It returns the escaped string.

7. What are the correct statements about superglobals?

a) “superglobals”¬†are always accessible, regardless of scope and we can
access them from any function, class or file without doing anything
special.
b) $_SESSION , $_COOKIE, $_ENV, $_FILES are the superglobal variables.
c) $GLOBALS is a PHP super global variable which is used to access global
variables from anywhere in the PHP script.
d) $_POST is not a superglobal veriable. It is used to collect form data after
submitting an HTML form with method=”post”.

8. Which of the following among A,B and C can be used to include comments in PHP?

A:   /* */
B: #
C: //

a) A and B only     b) A and C only    c) B and C only   d) All A,B and C

9. What is/are the correct statement(s)? about variables.

a) A variable name cannot start with a number.
b) A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ).
c) Variable names are case-sensitive ($age and $AGE are two different
variables)
d) A variable name must start with a letter or the underscore character.
e) A variable starts with the $ sign, followed by the name of the variable.

 

10. PHP is __________ scripting language.

a) sever-side
b) client-side
c) Middle-side
d) out-side ¬†ūüôā

11. PHP scripts are executed on _________.

a) Client compuer
b) Server computer
c) Depends on PHP code
d) None

12. Which of the following delimiting method is known as
string 
Interpolation.

a) delimitted by single quotes.
b) delimitted by <<< identifier.
c) delimitted by single quotes.
d) All of above.

13. In PHP, which datatypes are treaded as arrays.

a) string
b) integer
c) float
d) booleans

14. What is the output of fllowing script would be.

$var=15;
function ad it () {
GLOBAL $var;
$var++ ;
echo “value is $var”;
}
addit ();

a) value is 15
b) value is $var
c) value is 1
d) value is 16

16. Variable scope on which a variable does not loose its value when the
       function exists and use that value if the function is called again is.

a) local
b) static
c) function parameter
d) none of above

17. The left association operator % is used in PHP for

a) percentage
b) division
c) bitwise AND
d) modulus

18. Which of the following functions require the allow-url-fopen must
be 
enabled?

a) include()
b) require()
c) both (a) and (b)
d) None

19. On failure of which statement the script execution stops displaying
       error/warning message?

a) include()
b) require()
c) both (a) and (b)
d) None of above

20. Trace the false statement

a)  Any code found within an included file will inherit the variable scope of
the location of its caller

b)  Because the included code will be embedded in a PHP execution block,
the PHP execution block, the PHP escape tags (<?php?>¬†¬†aren’t
required on the file to be included.

c)  For the inclusion of remote files the allow-url-pope must be enabled ad
URL wrapper must be supported

d)  Including a file produces the same result as copying the data from the
file specified into the location in which thestatement appears.

21. correct statements?

a) str_replace() function replaces some characters with some other
characters in a string.
b) str_replace() function is case-sensitive.
c) str_ireplace() function use to perform a case-insensitive search.
d) syntax of this function is str_replace(find,replace,string,count). (count is
a variable that counts the number of replacements)

 

 

ANSWERS

1.Answer; All(a,b,c,d).
2.Answer; a.
3.Answer; a.
4.Answer; All.
5.Answer; b.
6.Answer; a,c,d
7.Answer; a,b,c
8.Answer; d.
9.Answer; a,b,c,d,e.
10.Answer; a.
11.Answer; b.
12.Answer; b.
13.Answer; a.
14.Answer; d.
16.Answer; b.
17.Answer; d.
18. Answer; c.
19. Answer; b.
20. Answer; b.
21. Answer; a,b,c,d.