小编Rya*_*mes的帖子

如何从Javascript数组中获取两个最大的整数并将值返回到DOM?

我写了一个解决方案来获取通过表单输入的整数列表.有用.它为您提供了两个最大整数的总和,并将其发布在DOM中.但是,对于100万个整数的大型数组来说,它并不是非常有效.

如何才能提高此解决方案的效率.

App.js

 // 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)

javascript arrays sorting performance

7
推荐指数
1
解决办法
603
查看次数

标签 统计

arrays ×1

javascript ×1

performance ×1

sorting ×1