Taiyuan Noodles Share: How to use JS to implement the function of returning the nth value of a Fibonacci sequence

Source: Internet
Author: User

Interview save experience, let's go!

On the occasion of the Entrance examination, restless I again double 叒 Corporation set out to interview save experience, went to the company confessed a process, the interviewer dumped me a piece of A4 paper, which wrote a JS algorithm pen test (I did not know this is in the study JS algorithm), which says "1, 1, 2, 3, 5, 8 ..., to find the value of the nth number "

I have to admit that at the time I first saw this problem in the brain is crazy. Later only remembered, this is not the math problem in the Fibonacci (Fat PO concubinage) Series It! Starting with the third number, each number is the first two numbers.

Get to this point, you've done half of it. The other half is that you need to turn the logic of the mathematical formula into the JS program logic.

In fact, the question can be changed to ask: to implement a function, enter a number n can return the Fibonacci sequence of the nth value.

Analysis Ideas

First of all, the idea is very important, let us together to analyze, the approximate idea is this:

First of all, we have to separate the special parts to make a judgment, which numbers are special? It is clearly the first two of the Fibonacci sequence, and the first two of the Fibonacci sequence are 1. Then define three variables, Firstnum, Secondnum, Total, respectively, representing the first number, the second number, and the sum of the two.

Then through a for-loop traversal, the result of Firstnum plus Secondnum is assigned to total, then the value of Secondnum is assigned to Firstnum, the value of total is assigned to Secondnum, In this way, according to the incoming N to continue to loop overlay, to achieve the desired total value, and finally return to go out.

After the idea is finished, let's use JS to realize it:

//It's probably the most common solution.varSeries =function (n) {varsum = [0,1]; if(N <2) {    returnSum[n]; }  varFirstnum =0; varSecondnum =1; varTotal =0;  for(vari =2; i<= N; i++) { Total= Firstnum +Secondnum; Firstnum=Secondnum; Secondnum=Total ; }  returnTotal ;}

The optimal solution of the interview question

Remember, the interviewer and our candidate's thinking is different, you apply for most of the time you are thinking, this question I will not do, can make, and they think is the best solution of the problem.

The work of the front-end, "optimal solution" is actually a self-pursuit of progress performance.

In addition to the above approach, what better way to solve it? The answer is yes.

Let's take a look at the solution of the iteration

var series = function (n) {  var feipo = [0,1];    for (var i=2; i<=n;i++) {    = feipo[i-1] + feipo[i-2]  }   return feipo[n];} // Console.log (Series (6));

Let's take a look at the recursive solution.

var series = function (n) {  if2) {    return series (n  1) + series (n2)  }else  {    return  N;}  } // Console.log (Series (6));

Which is the best solution, I believe that after reading the article you already have the answer.

Perhaps you will ask:

Did you make that leap in the written test?

You guess ~

Write it in the back.

So far I have participated in a lot of big and small front-end interviews, and indeed have heard that many interviewers will ask some algorithms. This is usually related to linked lists, trees, strings, and arrays of knowledge. The front-end interview to the algorithm requirements are not high, it seems that the industry has been a consensus. Although the algorithm good front-end interview will certainly add points, but only by common face test, but not to contact the demand, it is difficult to let people feel that the algorithm for the front-end is really important.

Until this day, Taiyuan, the company's front-end leader to me out of such a JS algorithm problem, but also talked to me a lot of content, and my inherent thinking has produced a strong collision. The interviewer also told me that their company's technical director is a Microsoft origin, pay attention to the algorithm this piece, he originally applied for in the time, but also the algorithm of investigation.

Although the interview was pass, but I think engineers should learn the algorithm, I think it is not only the development of software engineering ideas, but also a mental exercise.

Article preview: More front-end interview sharing I will be the first time to update in my public number < Uncle Earth > inside, welcome attention!

Taiyuan Noodles Share: How to use JS to implement the function of returning the nth value of a Fibonacci sequence

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.