evaluate type of variable and return sum of an array - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

evaluate type of variable and return sum of an 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  .j a v a2 s .  co  m
var array = [1, 2, 3, "test", 5, false];
var sum = function(arr) {
    var a = 0;
    var b = arr.length;
    for ( c = 0; c < b; c++ ) {
        if ( !isNaN(arr[c]) ) {
            a += arr[c];
        }
    }
    return a;
}
console.log(sum(array));
    }

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

Related Tutorials