This is the 27th day of my participation in the August More Text Challenge

Persistence in learning, notes is the soul, review the old to learn new, from time to time, review the knowledge point, deepen memory, get twice the result with half the effort!

Earlier we learned about JavaScript’s date-time library & Date object

Today we are going to learn about object based programming JavaScript functions – recursion

1. JavaScript functions – recursion

There is no doubt that functions are the foundation of JavaScript programming.

We create a function function using the expression at the beginning of the function keyword,

In addition, the arrow function in ES6: () => {function body} is also a representation of a function.

2. Recursive functions

In the work often need to deal with data, such as algorithms, related to the tree structure of the data structure algorithm, using recursion can be very simple to achieve. That’s where recursion comes in.

2.1 definition:

Functions call themselves, functions that call themselves we call recursive functions.

2.2 Problems needing attention in recursion

Of course, because it is their own call themselves, this time to consider the infinite loop, and bring the problem of memory consumption. A judgment call termination is generally required to prevent stack overflow due to too many calls.

For example, if the function calls itself tens of thousands of times, consider the error that may result: “Maximum call-stack size exceeded”.

2.3 the sample

Let’s take a number to the N power (power). For example, the following exponentiation function is implemented:

/** * exponentiates the function *@param {baseNumber} Number of bases *@param {exponent} The power frequency *@returns* /
function exponentiation (baseNumber, exponent) {
  if(exponent == 0) {
     return 1;
  } else {
    return baseNumber * exponentiation(baseNumber, exponent - 1);
  }
console.log(exponentiation(2.3));/ / -- > 8
Copy the code

This implementation arguably describes the concept more clearly than its looping version. This function calls itself multiple times with smaller exponents to multiply repeatedly.

2.4 Efficiency of recursion and loop

One problem with this implementation is that it is about a third as fast as the looping version in classic JavaScript implementations. And running through a simple loop is usually cheaper than calling the function multiple times.

The speed versus legibility dilemma is a classic one. You can see it as a kind of opposition and unity between being friendly to humans and being friendly to machines. Almost any program can be made faster by making it bigger and more complex. Programmers must make appropriate trade-offs.

In some functions, using the circular version is still fairly simple and easy to read. Replacing it with a recursive version doesn’t make much sense. Often, however, when programs deal with such complex concepts, it helps to give up some efficiency in order to make the program more straightforward.

Worrying about efficiency can be a distraction. Therefore, we always start by writing something that is correct and easy to understand. Since most of the code doesn’t execute very often at all, it’s not enough to spend any significant amount of time worrying about it being too slow, and you can measure it later and make improvements if necessary

2.5 Recursion also has efficient highlight moments

Recursion, however, is not always an inefficient alternative to loops. Some problems are easier to solve with recursion than with loops. In most cases, these issues require several “branches” to explore or deal with, and each branch may be extended to more branches again.

Consider this conundrum: Use recursion to do the following?

When starting with the number 1 and repeatedly adding 5 or multiplying by 3, you can produce a collection of an infinite number of numbers. Given a number, how do you write a function to find a series of such additions and multiplications that produce that number?

For example, the number 13 can be reached by first multiplying by 3 and then adding 5 twice, while the number 15 is impossible to reach at all.

Read more

Read more articles please check:

Date object, JavaScript string object based on object programming, Boolean type

[JS] Exception (error) processingtry-catch

Various magical bugs in [JS]

【 Data structure 】 data structure – objects and arrays (2) Array Array, 【 data structure 】 data structure – objects and arrays (1) Object, 【 data structure 】 in-depth understanding of JSON

Node.js file system server – emulated interface

[Node.js] file system module, [Node.js] HTTP module

[Node.js] efficiency tools – NVM & NRM, etc

Node.js Package management tool NPM and YARN

【Node.js】 Build automated development environment – basic introduction

[Node.js] Installation & Documentation, [Tool Preparation], [Start], [Detailed Steps (4)], [Module processing tools (5)], [Understanding of modular programming]

【Github】 Multi-user collaboration (2), 【Github】 Basic use (1)

【Git】 Code version control – Basic Operation (1)

Keep up the pace, step by step to the full stack!

Next we will continue to learn more about JavaScript related methods, chong Duck!! xdm

Learn the efficiency tools to improve development efficiency and empower our development!

Keep up with the pace and keep moving forward

Come on!! go~~