Return the index of largest sum within array - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Return the index of largest sum within array

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*from   w w w  .jav  a 2  s .c  o  m*/
var largestValue =0;
var locationOfLargest  =0;

sumArray = [1231231, 100, 909, 8, 998, 9238]

for(var i = 0; i<sumArray.length; i++) {
   if (!largestValue || sumArray[ i ] > largestValue) {
      largestValue = sumArray[ i ];
      locationOfLargest = i;
   }
}
console.log(locationOfLargest, largestValue);
    }

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials