Write a program to Sum of Digits in JQuery?


Write a program to accept a no. from user and display Sum of Digits.
(example: 214= 2+1+4 =  7)




<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>

  

<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
var x = 0;
var y = parseInt($("#sum").val());
while(y>0)
{
x = x + y % 10;
y = Math.floor(y/10);
}
alert("sum of digits " + x);

});

});


</script>
</head>

<body>

<div>
<h2>Sum of Digits</h2>
<input type = "text" id = "sum" placeholder="Enter your digit" >

<p id="demo"></p>

<button id="btn">Try it</button>
</div>
</body>
</html>


Comments

Popular posts from this blog

how to customize dashboard in active admin gem in rails 4

fibonacci series in Ruby

Simple calculator using JQuery and HTML..