Today Cisterndata Liu always showed me a few small problems she saw about the basics of programming. It feels so interesting! This is a small problem, because who will do it. Say they are difficult--it is true that most junior programmers now face this primary problem, and it's really hard to do it perfectly. Then the next few days, on each of these questions are rehearsed.
Question 1. Use the For loop, while loop, and recursion to write out 3 functions to calculate the sum of a given sequence.
The first is the For loop, which is the most basic calculation.
var arrdata = [1,2,3];var summary = 0;for (i = 0; i < arrdata.length; i++) { Summary + = parsefloat (Arrdata[i]);} Alert (summary);
The basic qualities that need to be examined here are: the programming habit of assigning initial values to variables. There is also the Parsefloat data type conversion.
Of course, if you can write Parsefloat (Arrdata[i], 10), add very much. You can use the tofixed (n) function and add 20 points.
The second is the while loop, which is not commonly used in a for loop in a program. But in dealing with some of the non-fixed-length cycle logic and so on, can be more flexible than for. Of course, the conditional part of the while can be written as a perpetual, causing the program to loop. This is the first problem that programmers must first circumvent. The instance code is as follows.
var arrdata = [1,2,3];var summary = 0;i = 0;while (i < arrdata.length) { Summary + = parsefloat (Arrdata[i]); i++;} Alert (summary);
A third method is recursion. Recursive methods should belong to the intermediate-level algorithm. For web programmers, although the recursive algorithm is a basic quality, but the possibility of use is not very large. During the interview, 20% of the interviewees were able to describe the algorithm correctly. Probably only half of it is written correctly. It is important to note that the recursive algorithm is not necessarily a matter of consideration when considering a problem. For example, the following is a question to consider from the forward.
var arrdata = [1,2,3];/** * uses the recursive algorithm to find the array and * @param arr is the sum of the array * @param n the nth value of the array, note that it is labeled n-1 */function sum (arr, n) { if (n > 0) { return parsefloat (arr[n-1]) + sum (arr, n-1); } else { return 0;} } Alert (sum (arrdata, arrdata.length));
The processing here is based on a habit of processing from the back forward. Write from the back, the program after a special case processing only 0. and 0 is a No. 0 value that does not exist in the index group. We just have to give it a number that has no effect on the result of the operation. It is not possible to calculate from the previous backwards. But there is one last problem to deal with, when legibility and operability become worse.
The fourth method is to perform string concatenation directly with Eval. This is a very common lazy method in the calculation of one-dimensional arrays.
var arrdata = [1,2,3];alert (eval (arrdata.join ("+")));
Finally, the actual validation page of the top 4 summation methods. Click here
Array summation of programming fundamentals