This is the 17th day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Thank you for meeting me. Hi, I’m Ken

Author: Please call me Ken link: juejin.cn/user/109118… Source: gold mining copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

🌊🌈

Part of the content and pictures of this article are from the Internet. If you have any questions, please contact me (there is an official account in the introduction of the homepage).

This blog is suitable for those who just come into contact with JS and want to review after a long time. This article is about “JavaScript functions”.

🌊🌈 About:

📚4.3_ Function synthesis case

📙4.3.1_ Use the function to find the maximum value of all parameters

function getMax(){
var max = arguments[0];

for(var i = 1; i < arguments.length; i++) {
if (arguments[i] > max) {
max = arguments[i]; }}return max;
}

console.log( getMax(1.2.3));// Output: 3
console.log( getMax(1.2.3.4.5));// Output: 5
console.log( getMax(11.2.34.Awesome!.5.100));// Output: 666
Copy the code

📗4.3.2_ Use functions to reverse the array element order

function reverse(arr){
var newArr = [];
for(var i = arr.length - 1; i >= 0; i--){
newArr[newArr.length] = arr[i];
}
return newArr;
}
var arr1 = reverse( [1.3.4.6.9]);console.log(arr1);// output :(5)[9,6,4,3,1]
Copy the code

📘4.3.3_ Using functions to judge leap years

function isLeapYear(year){
var flag = false;
if(year % 4= =0 && year % 100! =0 || year % 400= =0){
flag = true;
}
return flag;
}
console.log( isLeapYear(2020)?'2020 is a leap year ' : '2020 is not a leap year ' );// 2020 is a leap year
console.log( isLeapYear(2021)?'2021 is a leap year ' : '2021 is not a leap year ' );// 2021 is not a leap year
Copy the code

📒4.3.4_ Gets the number of days in February for the specified year

// Add 4.3.3 code to the answer to this question
function fn(){
var year = prompt('Please enter a year');
if ( isLeapYear(year) ){
alert('The current month is a leap year. February has 29 days.')}else{
alert('The current month is a normal year. February has 28 days.')}}function isLeapYear(year){
var flag = false;
if(year % 4= =0 && year % 100! =0 || year % 400= =0){
flag = true;
}
return flag;
}

fn();
Copy the code

📚4.4_ Function progression

📙4.4.1_ Function expressions

A function expression is an assignment of a declared function to a variable through which the function is called and the parameters are passed.

// Declare the function
function funcName() {
/ / the function body
}
Copy the code
var sum = function (num1, num2) {
// Function expression
return num1 + num2;
};
console.log ( sum(1.2));/ / 3
Copy the code

A function expression is defined in much the same way as a function declaration, except that a function expression must be defined before a call is made, whereas a function declaration does not limit the order in which it is declared and called. Since sum is a variable name and the function assigned to the variable has no function name, the function is also called anonymous.

The variable sum can be called as a function when an anonymous function is assigned to it.

📗4.4.2_ callback function

In a project, if you want a part of the function body to be determined by the caller, you can use a callback function. A callback is A function A passed as an argument to A function B, and then called within B’s body. At this point, we call function A the callback function. Among them, the anonymous function is often used as the function parameter passing, to realize the callback function.

function cal(num1,num2,fn){
return fn(num1,num2);
}
console.log( cal(45.55.function(a,b){
returna+b; }));console.log(cal(10.20.function(a,b){
returna*b; }));Copy the code

Take a look at callback functions and recursive calls

📘4.4.3_ Recursive calls

A recursive call is a special call in a nested call to a function.

It refers to the process by which a function calls itself inside another function. This function becomes a recursive function.

The difference between a recursive function and a callback function, that’s what you need to think about

It is important to note that recursive functions can only be used in specific situations, such as computing factorials.

function factorial(n){  // Define the callback function
if( n == 1) {return 1;
}
return n * factorial(n - 1);
}
var n = prompt('Find n factorial \n. N is a positive integer greater than or equal to 1. For example, 2 means find 2! . ');
n = parseInt(n);
if(isNaN(n)){
console.log('N input is not valid');
}else{
console.log(n + The factorial of 'is :'+factorial(n));
}
Copy the code

Oneself think of a way to do, for just learn small white, a little difficult

That’s the end of today’s introductory study

Peace

🌊🌈

Akken’s HTML, CSS guide for getting started (1)_HTML basics akken’s HTML, CSS guide for getting started (2)_HTML page elements and attributes Akken’s HTML, CSS guide for getting started (3)_ Text style attributes Aken’s HTML, CSS getting started guide (four)_CSS3 selector Aken’s HTML, CSS getting Started guide (five)_CSS box model Aken’s HTML, CSS getting Started guide (six)_CSS box model Aken’s HTML, CSS getting Started guide (seven)_CSS box model Akken’s HTML, CSS guide for getting started (eight)_CSS box model akken’s HTML, CSS guide for getting Started (nine)_ Floating and positioning Akken’s HTML, CSS guide for getting started (ten)_ Floating and positioning Akken’s HTML, CSS Guide for getting started (eleven)_ Floating and positioning Ken’s HTML, CSS guide for getting started (12)_ Form application of HTML, CSS guide for getting started (13)_ form application of HTML, CSS guide for getting started (14)_ Form application of HTML, CSS guide for getting started (15)_ Form application Ken o HTML, CSS, getting started guide (16) _ multimedia technology suggest collection 】 【 share | JS dry challenge the shortest time take you into the JS (a) suggest collection 】 【 share | JS dry challenge the shortest time take you into the JS (2) share | JS dry goods Recommended collection 】 challenge the shortest time take you into the JS (3) suggest collection 】 【 share | JS dry challenge the shortest time take you into the JS (4) suggest collection 】 【 share | JS dry challenge the shortest possible time to take you into the JS (5)

🌊🌈 About postscript:

Thank you for reading, I hope it can help you if there are flaws in the blog, please leave a message in the comment area or add contact information in the personal introduction of the home page

Original is not easy, “like” + “follow” thanks for supporting ❤