我写了一个解决方案来获取通过表单输入的整数列表.有用.它为您提供了两个最大整数的总和,并将其发布在DOM中.但是,对于100万个整数的大型数组来说,它并不是非常有效.
如何才能提高此解决方案的效率.
// This function reverses the order of the array and places the biggest numbers first
function sortNumber(a, b) {
return b - a;
}
// this function is used to ensure the user didn't enter any letters
function getArray() {
var alphaExp = /^[a-zA-Z]+$/;
// This function takes the array, orders it, adds the sum of the two largest numbers and returns the value
function sumOf(x) {
// Sort the ary with the sortNumber function
array.sort(sortNumber);
// …Run Code Online (Sandbox Code Playgroud)