Add digit from a number - Javascript Data Type

Javascript examples for Data Type:Number

Description

Add digit from a number

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width"> 
   </head> 
   <body> 
      <script>
var addDigits = function a(num) {
     var length=num.toString().length;
     var value=num.toString();/*w  ww . j ava2 s. co  m*/
     var sum=0;
     for(var i=0;i<value.length;i++) {
         sum+=Number(value[i]);
     }
     return sum;
};
console.log(addDigits(123456789));

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

Related Tutorials