最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - sum of digits of a number - Stack Overflow

programmeradmin3浏览0评论

I want to make a program with JavaScript which adds the digits of a number until the number of digits in the result is 1.

Example:

57 = 5 + 7 = 12 = 1 + 2 = 3.

I have made a program which does half of the job. Here is it

<!DOCTYPE html>
<html>

<head>
    <title>Try</title>
    <style type="text/css">

    </style>
</head>

<body>
    <form><input type="text" onkeyup="sumofDigits(this.value)"></input>
    </form>
    <h1></h1>
    <script type="text/javascript">
        function sumofDigits(number) {
            var num = number.toString();
            var sum = 0;

            for (var i = 0; i < num.length; i++) {
                sum += parseInt(num.charAt(i), 10);
            }

            document.querySelector('h1').innerHTML = sum;
        }
    </script>
</body>

</html>

I want to make a program with JavaScript which adds the digits of a number until the number of digits in the result is 1.

Example:

57 = 5 + 7 = 12 = 1 + 2 = 3.

I have made a program which does half of the job. Here is it

<!DOCTYPE html>
<html>

<head>
    <title>Try</title>
    <style type="text/css">

    </style>
</head>

<body>
    <form><input type="text" onkeyup="sumofDigits(this.value)"></input>
    </form>
    <h1></h1>
    <script type="text/javascript">
        function sumofDigits(number) {
            var num = number.toString();
            var sum = 0;

            for (var i = 0; i < num.length; i++) {
                sum += parseInt(num.charAt(i), 10);
            }

            document.querySelector('h1').innerHTML = sum;
        }
    </script>
</body>

</html>
Share Improve this question edited Mar 5, 2017 at 6:38 asked Feb 28, 2016 at 5:27 user5730434user5730434 2
  • 1 digits of a number and then the digits of the result until the number of digits of result is 1 ? Could not read it.. – Rayon Commented Feb 28, 2016 at 5:34
  • Check the question now – user5730434 Commented Feb 28, 2016 at 5:37
Add a ment  | 

4 Answers 4

Reset to default 7

There is no need of string functions here. You can simply arrive the result with help of mod(% operation.

function sumDigits(number) {
      document.querySelector('h1').innerHTML = ((number-1)%9+1);
}

This does it.

function sumDigits(number) {
    var num = number.toString();

    while (num.length != 1) {
        var sum = 0;
        for (var i = 0; i < num.length; i++) {
            sum += parseInt(num.charAt(i), 10);
        }
        num = sum.toString();
        document.querySelector('h1').innerHTML = sum;
    }
}
function sumDigits(number) {
    var str = number.toString();
    var sum = 0;

    for (var i = 0; i < str.length; i++) {
        sum += parseInt(str.charAt(i), 10);
    }

    if (sum > 9)  
        sumDigits(sum);
      else           
        document.querySelector('h1').innerHTML = sum;

}

Try this

<script type="text/javascript">
    function sumDigits(number) {
        while( number > 9){
            var num = number.toString();
            var sum = 0;
            for (var i = 0; i < num.length; i++) {
                sum += parseInt(num.charAt(i), 10);
            }
            number = sum;
        }
        document.querySelector('h1').innerHTML = sum;
    }
</script>
发布评论

评论列表(0)

  1. 暂无评论