JS Essay 5

Source: Internet
Author: User
Tags rounds

Function

A function is a set of statements that perform a function, defined by a keyword function name plus a set of parameters; Functions can be called repeatedly after they are defined, often written as a function, using functions to make the structure of the Code more
More clarity

document.write (1+"<br>");d Ocument.write (5+"<br>");d Ocument.write (3+"<br>");d Ocument.write (Ten+"<br>");d Ocument.write (5+"<br>" );//We can use the function to implement the above codefunction DW (output) {document.write (Output+"<br/>"); } DW (1); DW (5); DW (3); DW (Ten); DW (5);

Return a value from a function using the return statement

function sum (NUM1, num2) {       return num1 + num2;     }      var s = sum (12);     

Note: Any code that is behind the return statement will never execute! In JavaScript, you don't have to specify a return value!

A variable can be either global or local.
Global variables: can be referenced anywhere in the script, once you declare a global variable in a script, you can reference it anywhere in the script (including inside the function), the scope of the global variable is the entire script; Local variables: Only exist within the function that declares it, cannot be used outside of the function, and the scope of the local variable is limited to the inside of the function.

// Global Variables            var 1 ;             //  ...            var 2 ;                         // Local Variables             function func () {                console.log (a);                 var 3 ;                Console.log (a);            }            Func ();                        Console.log (a);

PS: Variables declared in the function with VAR are defined in the function, but are not declared with VAR is the global variable is defined in the function, with VAR declaration is the local variable

1.Date Date Object
Date objects are used to process dates and times.

var mydate =NewDate ();        Mydate.getyear (); //get Current year (2-bit)Mydate.getfullyear ();//Get the full year (4-bit, 1970-????) Mydate.getmonth ();//Get the current month (0-11, 0 for January)Mydate.getdate ();//get current day (1-31)Mydate.getday ();//get Current week x (0-6, 0 for Sunday)Mydate.gettime ();//Gets the current time (the number of milliseconds starting in 1970.1.1) mydate.gethours (); //gets the current number of hours (0-23)Mydate.getminutes ();//gets the current number of minutes (0-59)Mydate.getseconds ();//gets the current number of seconds (0-59)Mydate.getmilliseconds ();//gets the current number of milliseconds (0-999) mydate.tolocaledatestring (); //Get Current datevar mytime=mydate.tolocaletimestring ();//Get current TimeMydate.tolocalestring ();//get date and time
New Date (),                     // object. Method ()                    document.write (time);

New Date (),                 = d.getfullyear (),                = D.getmonth () + 1,                = d.getdate (),                = d.gethours (),                 = d.getminutes (),                = d.getday (),                = [' Sunday ', ' Monday ', ' Tuesday ', ' Wednesday ', ' Thursday ', ' Friday ' , ' Saturday ');                 + "Year" + M + "month" + D + "Day" + H + ":" + i + "" + week[wd]);

2.Math objects are used to handle complex mathematical operations

The Math object is a global object of JavaScript and does not need to be created   with new Math.Abs (-2);           The method can return the absolute value of a number.   Math.Round (5.5);        This method rounds a number to a close integer. Math.random ();           A random number between 0 (inclusive) ~ 1(not included). Math.ceil (1.4);         The Ceil () method rounds up a number. Math.floor (1.6);        The floor () method returns a large integer that is less than or equal to X.  Math.pow (4,3);          

The value of the random number:

// 0-5            var result = parseint (Math.random () * 5);                         // 5-10            var result = parseint (5+math.random () * 5);                         // 0-10            var result = parseint (Math.random () *);                         // 0-100            var result = parseint (Math.random () * +);                         // 50-100            var result = parseint (+ math.random () * 50);

We can do a lottery program with random values:

var cj=parseint (Math.random () *10);                 if (Cj==1) {                    alert (' The number you have drawn is: ' +cj+ ' + ' Maldives 7 people 7th Tour ');                }                 Else {                    alert (' The number you have drawn is: ' +cj+ ' + ' Thank you for participating ');                }

3.Array Array Object
Our variables generally only store one content so it's actually a single container. Our arrays can generally have one or more values, so the array is a large container
Component: An array is actually a multi-container array element consisting of multiple (key values) that consists of a pair (key value) and our array is an array of arrays consisting of multiple array elements by default starting at 0.

There is a saying in front.

 //3 ways to define an arrayvar arr =NewArray (23,23,45,56,435); var arr2= [23,34,546]; var arr3=NewArray (); arr3[0] = 234; arr3[1] = 234; //gets the length of the array. Length document.write (arr.length);//gets the value corresponding to the array subscript document.write (arr2[0]); //array element Additionsvar Arr_len = Arr.push (' abc ', ' EFG '); //adds one or more new elements to the end of the array and returns the new length of the arrayvar Arr2_len = arr2.unshift (' abc '))//adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, returning the new length of the arrayArrayobject.splice (index,howmany,item1,....., ItemX)//index required.   An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array. //Howmany required. The number of items to delete.   If set to 0, the item is not deleted. //item1, ..., ItemX Optional.   Adds a new item to the array. Arr3.splice (0,0, "abc");//inserts one or more new elements into the specified position of the array, the elements of the insertion position are automatically moved back, and the "" is returned. document.write (arr3[0]); //array element Deletionvar del = ["AA", 23,345,56,34, "BB"]; var del_last=Del.pop (); //removes the latter element and returns the element valuevar Del_first =Del.shift (); //removes the previous element and returns the element value, and the elements in the array are automatically moved forwardvar Del_arr = Del.splice (0,2); //removes the specified number of DeleteCount elements from the specified position deletepos, returning the removed element as an array//document.write (Del_arr);//interception and merging of arrays//array intercept var merge = [' AA ', ' BB ', 45,54, ' CC ']; var Merge_len = Merge.slice (0,2); //Returns the part of an array as an array, the star T start position end position does not include the end item//Merge array var a = [n/a]; //document.write (A.concat (4,5));//array element sort var arr_sort = [' AA ', 23,45,4456,23];var res_sort =arr_sort.reverse ();//invert the element (the line of money to the back, after the row to the front), return the array addressvar Num_arr = [23,11,45,56,56]; //sort alphabetically or by number sizevar res_num =Num_arr.sort (); //document.write (res_num);//array elements converted to string and string conversions to arraysvar arr4 = [' AA ', ' BB ', 23,232,435,34]; var Str_arr= Arr4.join ("-"); //returns a string that connects each element value of an array together with the middle-separated. //document.write (Str_arr);//string conversions to arraysvar test = "ASD,ASD,ASD,ASD,FG,FGF"; var arrtest= Test.split (","); //Convert to ArraysConsole.log (arrtest.length);//traversal of an arrayvar demo = [33,23,12,3,343]; var i=0;  while(i<demo.length) {document.write (Demo[i]+ "<br>"); I++; }    Do{document.write (Demo[i]+ "<br>"); I++; } while(i<demo.length);  for(Var i=0;i<demo.length;i++) {document.write (Demo[i]+ "<br>"); } 

Take a look at the short point:

var arr = [                "Heading 1",                                "Heading 2", "Heading 3",                "Heading 4"            ];            Arr.push ("Heading 5", "Heading 6");            Arr.unshift ("Heading 7", "Heading 8");  for (var i = 0; i < arr.length; i++) {                    document.write ("<li>" +arr[i]+ "</li>");                }

JS Essay 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.