Preface
An issue encountered in the interview: the JS array sum function. The first thing I think of is array loops. However, I think the interviewer asked this question not to take the well-known method. At that time, I was so clever that I thought of the recursive function constantly adding items to the array, but I couldn't adjust the method after a long time. It turned out that this was not the optimal solution. Finally, the interviewer asked me if I had seen reduce() before, but it was really not. So when I look up the information, Array.reduce() is a new attribute added to ES5, and similar ones are Array.reduceRight()。
Let’s summarize the method of summing arrays below.
The roughest way: loop acquisition
Add one by one through the for loop. Look at the code:
Array.prototype.sum = function (){ var result = 0; for(var i = 0; i < this.length; i++) { result += this[i]; } return result;};[1,4,7,2,10].sum(); // 24Use reduce method
Using the reduce method, you can write a sum method for summing an array.
reduce() method receives a function as an accumulator, and each value in the array (from left to right) starts to shrink and ends up being a value.
Syntax of reduce:
array.reduce(callback[, initialValue]);
The callback function accepts 4 parameters: previousValue (the value returned by the last callback), currentValue (the current element being processed), index (index), and the array itself (the first parameter of callback is called for the first time), and executes the function for each value in the array.
The initialValue parameter is optional, indicating the initial value; if initialValue parameter is specified, it is regarded as the previous value originally used. If default, the first element of the array is used as previous initial value, and current is one place in the back.
Array.prototype.sum = function (){ return this.reduce(function (partial, value){ return partial + value; })};[1,4,7,2,10].sum(); // 24 Compared with the first method, using reduce() method is more efficient.
The efficiency comparison of these two methods can directly call new Date() before and after the function is run to obtain the real-time time, thereby comparing the execution time through the time difference. I won’t compare it here because everyone’s execution environment varies greatly. The test result is that the execution time of reduce() method is shorter.
JS array sum function and find the maximum value in the array
Example code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>Wulin.net_js array summation summax method_Wulin.net</title><meta name="keywords" content="Webmaster, web page special effects, web page special effects code, js special effects, js scripts, scripts, advertising codes, VeVB.COM, www.VeVB.COM, Wulin.com" /><meta name="description" content="www.VeVB.COM, Wulin.com, webmasters must have js special effects and advertising codes. A large number of high-quality js special effects, providing high-quality advertising code downloads, all on Wulin.com" /></head><body><a href="//www.VeVB.COM/">Wulin.com</a>, webmasters must have high-quality web page special effects and advertising codes. VeVB.COM, webmaster js special effects. <hr><script type="text/javascript">//Sum Array.prototype.sum = function () { for (var sum = i = 0; i < this.length; i++)sum += parseInt(this[i]); return sum ;};//Search maximum value Array.prototype.maxima = function () { for (var i = 0, maxValue = Number.MIN_VALUE; i < this.length; i++)parseInt(this[i]) > maxValue && (maxValue = this[i]); return maxValue;};//Apply var arr = [1,21,3,4,22,45,60,7,32];alert(arr.join("+") + "=" + arr.sum()); alert(arr.join("|") + ", the largest number is:" + arr.maxima());</script></body></html>The above is all the content of this article. I hope it will be helpful to everyone's use of JavaScript. If you have any questions, please leave a message and discuss it. The editor will reply to everyone in time.