JS Basic Learning 04

Source: Internet
Author: User

15. Bubble sort

Definition: Bubbling sorting is an algorithm that tells the data to be arranged from large to small or from smallest to largest.

Method: Through the loop statement to achieve, the specific implementation way is: The value of the array, followed by the value of the comparison, if the results of the comparison meet certain requirements, then the two 22 exchange until the end of the loop to complete the sorting. Such as:

Bubble sort, small to large [65,97,76,13,27,49,58]

var arr = [65,97,76,13,27,49,58,1,12,26];
var m;

Method One:
for (var i = arr.length-1; i > 0;i--) {
for (var j = 0;j < i;j++) {
if (Arr[j] > arr[j+1]) {
m = Arr[j];
ARR[J] = arr[j+1];
Arr[j+1] = m;
}
}
}
Console.log (arr);
Method Two:
for (var i = 0;i<arr.length-1;i++) {
for (var j = 0;j<arr.length-1-i;j++) {
if (Arr[j] > arr[j+1]) {
m = Arr[j];
ARR[J] = arr[j+1];
Arr[j+1] = m;
}
}
}
Console.log (arr);

16. Function 16.1 What is a function

A function is actually a piece of code, a block of code that executes when called, and can be reused.

Function: The reusable code can be extracted into a function, and the non-reusable part as a parameter passed into the function.

Definition of the function: (There are two ways to define the function, one for the time being explained first)

Define a function using the function declaration:

Function name (parameter) {

function body (block of code to be executed);

return value;

}

After the function declaration, the function itself is not executed automatically, and we need to call it by means of the function name ().

The benefits of a function: a declaration or a definition that can be called repeatedly.

16.2 Composition of functions

Three elements of a function: function name, parameter, return value.

Function name: The naming of functions is usually a verb, which represents function.

Parameter: The parameter is not required and the function can have no arguments.

Return value: The return value is the same for each function, but if the function does not have an explicit return value, the default return value is undefined.

Function Execution Process: We call the function by means of "function name ()", the function will assign the argument value we give to the corresponding parameter in the function (if the function has parameters), then execute the statement within the function, and return the result of execution to us. The data we provide to the function is the parameter, and the last data returned to us by the function is the return value.

The relationship between the 16.3 parameter and the argument

Formal parameters:

For example: function Getsum (a,b,c) {}, the parameters used when defining functions are called formal arguments, occupy positions and function within functions.

Actual parameters:

such as: Getsum (10,20), when the function is actually called, the incoming data is called the argument.

The relationship between the two: in JS, the number of arguments and parameters can be different.

The usage of 16.4 ruturn

In the function, return is generally used to return the data in the function, and if the return is followed by the data, the subsequent data is returned and undefined is returned if there is no data behind the return. And after the program runs the return statement, it immediately jumps out of the current function, and the code behind it is not executing.

Such as:

function Getsum (A, b) {
A = 100;
return a + B; Return the calculated result with the keyword return
Console.log ("See I did not .... "); The return statement immediately jumps out of the function, and the subsequent code is no longer executed.
}
var num = Getsum (A, b);

16.5 Nesting of functions

The nesting of functions is the invocation of other functions in one function, while the order in which the statements are executed is executed in strict accordance with the principle of top-to-bottom execution. Such as:

Function F1 () {

Console.log (" This is the function F1");

}

function F2 () {

Console.log (" This is the function F2");

F1 ();

Console.log (" This is the function F2");

}

F2 ();

The results shown are:

This is the function F2

This is the function F1

This is the function F2

When the function F2 () is called, the first statement in F2 () Console.log ("This is a function F2") is executed, and the second sentence F1 () is invoked, and the F1 () function is called, which jumps to the F1 () function, executes the F1 () function in the statement console.log (" This is the function F1 "), and after the F1 () function finishes, go back to the F2 () function and continue execution from the next statement of the call to the F1 () function until the F2 () function finishes executing.

JS Basic Learning 04

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.