javaScript - Find the sum of all divisors of a given integer











up vote
0
down vote

favorite












i'm doing some coding exercises and i'm not being able to solve this one.



Find the sum of all divisors of a given integer.
For n = 12, the input should be
sumOfDivisors(n) = 28.



example: 1 + 2 + 3 + 4 + 6 + 12 = 28.



Constraints:
1 ≤ n ≤ 15.



how can i solve this exercise? i'm not being able to.



function(n){
var arr = ,
finalSum;

if(n <= 1 || n => 16){
return false ;
}
for(var i = 0; i < n; i++){
var tmp= n/2;
arr.push(tmp)
// i need to keep on dividing n but i can't get the way of how to
}
return finalSum;
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    i'm doing some coding exercises and i'm not being able to solve this one.



    Find the sum of all divisors of a given integer.
    For n = 12, the input should be
    sumOfDivisors(n) = 28.



    example: 1 + 2 + 3 + 4 + 6 + 12 = 28.



    Constraints:
    1 ≤ n ≤ 15.



    how can i solve this exercise? i'm not being able to.



    function(n){
    var arr = ,
    finalSum;

    if(n <= 1 || n => 16){
    return false ;
    }
    for(var i = 0; i < n; i++){
    var tmp= n/2;
    arr.push(tmp)
    // i need to keep on dividing n but i can't get the way of how to
    }
    return finalSum;
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      i'm doing some coding exercises and i'm not being able to solve this one.



      Find the sum of all divisors of a given integer.
      For n = 12, the input should be
      sumOfDivisors(n) = 28.



      example: 1 + 2 + 3 + 4 + 6 + 12 = 28.



      Constraints:
      1 ≤ n ≤ 15.



      how can i solve this exercise? i'm not being able to.



      function(n){
      var arr = ,
      finalSum;

      if(n <= 1 || n => 16){
      return false ;
      }
      for(var i = 0; i < n; i++){
      var tmp= n/2;
      arr.push(tmp)
      // i need to keep on dividing n but i can't get the way of how to
      }
      return finalSum;
      }









      share|improve this question













      i'm doing some coding exercises and i'm not being able to solve this one.



      Find the sum of all divisors of a given integer.
      For n = 12, the input should be
      sumOfDivisors(n) = 28.



      example: 1 + 2 + 3 + 4 + 6 + 12 = 28.



      Constraints:
      1 ≤ n ≤ 15.



      how can i solve this exercise? i'm not being able to.



      function(n){
      var arr = ,
      finalSum;

      if(n <= 1 || n => 16){
      return false ;
      }
      for(var i = 0; i < n; i++){
      var tmp= n/2;
      arr.push(tmp)
      // i need to keep on dividing n but i can't get the way of how to
      }
      return finalSum;
      }






      javascript math






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 31 '17 at 22:21









      Franco Manzur

      108112




      108112
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          This is another way to do it:






          var divisors = n=>[...Array(n+1).keys()].slice(1)
          .reduce((s, a)=>s+(!(n % a) && a), 0);

          console.log(divisors(12));





          JSFiddle: https://jsfiddle.net/32n5jdnb/141/



          Explaining:





          • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.


          • Array(n+1) creates an empty array of n+1 elements


          • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence


          • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence


          • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?


          • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation


          • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {


          • s+(!(n % a) && a) is the calculation of the value.


          • s+ s (for sum) or the last value calculated +


          • !(n % a) this returns true only for the elements that have a 0 as modular value.


          • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.


          • , 0 is the initial value for the reduce calculation.


          Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce



          Edit



          Answering to Tristan Forward:






          var divisorsList = ;
          var divisors = (n)=>[...Array(n+1).keys()].slice(1)
          .reduce((s, a)=>{
          var divisor = !(n % a) && a;
          if (divisor) divisorsList.push(divisor);
          return s+divisor;
          }, 0);


          console.log('Result:', divisors(12));
          console.log('Divisors:', divisorsList);








          share|improve this answer



















          • 3




            Incredible readability.
            – destoryer
            Mar 31 '17 at 23:25






          • 1




            I haven't finished yet! :D
            – Nelson Teixeira
            Mar 31 '17 at 23:27








          • 1




            Now it's finished! take a look :)
            – Nelson Teixeira
            Mar 31 '17 at 23:28








          • 2




            @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
            – Nelson Teixeira
            Apr 1 '17 at 16:01






          • 2




            Created an explanation of the code to make it better :)
            – Nelson Teixeira
            Apr 1 '17 at 16:27


















          up vote
          3
          down vote













          You have to check if specified number is or not a divisor of given integer. You can use modulo % - if there's no rest, specified number is the divisor of the given integer - add it to the sum.






          function sumDivisors(num){
          var sum = 0;
          for (var i = 1; i <= num; i++){
          if (!(num % i)) {
          sum += i;
          }
          }
          console.log(sum);
          }

          sumDivisors(6);
          sumDivisors(10);








          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43150520%2fjavascript-find-the-sum-of-all-divisors-of-a-given-integer%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            10
            down vote



            accepted










            This is another way to do it:






            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            JSFiddle: https://jsfiddle.net/32n5jdnb/141/



            Explaining:





            • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.


            • Array(n+1) creates an empty array of n+1 elements


            • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence


            • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence


            • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?


            • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation


            • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {


            • s+(!(n % a) && a) is the calculation of the value.


            • s+ s (for sum) or the last value calculated +


            • !(n % a) this returns true only for the elements that have a 0 as modular value.


            • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.


            • , 0 is the initial value for the reduce calculation.


            Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce



            Edit



            Answering to Tristan Forward:






            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);








            share|improve this answer



















            • 3




              Incredible readability.
              – destoryer
              Mar 31 '17 at 23:25






            • 1




              I haven't finished yet! :D
              – Nelson Teixeira
              Mar 31 '17 at 23:27








            • 1




              Now it's finished! take a look :)
              – Nelson Teixeira
              Mar 31 '17 at 23:28








            • 2




              @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
              – Nelson Teixeira
              Apr 1 '17 at 16:01






            • 2




              Created an explanation of the code to make it better :)
              – Nelson Teixeira
              Apr 1 '17 at 16:27















            up vote
            10
            down vote



            accepted










            This is another way to do it:






            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            JSFiddle: https://jsfiddle.net/32n5jdnb/141/



            Explaining:





            • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.


            • Array(n+1) creates an empty array of n+1 elements


            • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence


            • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence


            • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?


            • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation


            • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {


            • s+(!(n % a) && a) is the calculation of the value.


            • s+ s (for sum) or the last value calculated +


            • !(n % a) this returns true only for the elements that have a 0 as modular value.


            • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.


            • , 0 is the initial value for the reduce calculation.


            Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce



            Edit



            Answering to Tristan Forward:






            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);








            share|improve this answer



















            • 3




              Incredible readability.
              – destoryer
              Mar 31 '17 at 23:25






            • 1




              I haven't finished yet! :D
              – Nelson Teixeira
              Mar 31 '17 at 23:27








            • 1




              Now it's finished! take a look :)
              – Nelson Teixeira
              Mar 31 '17 at 23:28








            • 2




              @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
              – Nelson Teixeira
              Apr 1 '17 at 16:01






            • 2




              Created an explanation of the code to make it better :)
              – Nelson Teixeira
              Apr 1 '17 at 16:27













            up vote
            10
            down vote



            accepted







            up vote
            10
            down vote



            accepted






            This is another way to do it:






            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            JSFiddle: https://jsfiddle.net/32n5jdnb/141/



            Explaining:





            • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.


            • Array(n+1) creates an empty array of n+1 elements


            • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence


            • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence


            • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?


            • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation


            • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {


            • s+(!(n % a) && a) is the calculation of the value.


            • s+ s (for sum) or the last value calculated +


            • !(n % a) this returns true only for the elements that have a 0 as modular value.


            • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.


            • , 0 is the initial value for the reduce calculation.


            Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce



            Edit



            Answering to Tristan Forward:






            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);








            share|improve this answer














            This is another way to do it:






            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            JSFiddle: https://jsfiddle.net/32n5jdnb/141/



            Explaining:





            • n=> this is an arrow function, the equivalent to function(n) {. You don't need the () if there's only one parameter.


            • Array(n+1) creates an empty array of n+1 elements


            • .keys() gets the keys of the empty array (the indexes i.e. 0, 1, 2) so this is a way to create a numeric sequence


            • [...Array(n+1)].keys()] uses the spread (...) operator to transform the iterator in another array so creating an array with the numeric sequence


            • .slice(1) removes the first element thus creating a sequence starting with 1. Remember the n+1 ?


            • .reduce() is a method that iterates though each element and calculates a value in order to reduce the array to one value. It receives as parameter a callback function to calculate the value and the initial value of the calculation


            • (s, a)=> is the callback function for reduce. It's an arrow function equivalent to function(s, a) {


            • s+(!(n % a) && a) is the calculation of the value.


            • s+ s (for sum) or the last value calculated +


            • !(n % a) this returns true only for the elements that have a 0 as modular value.


            • (!(n % a) && a) is a js 'trick'. The case is that boolean expressions in javascript don't return true or false. They return a 'truthy' or 'falsy' value which is then converted to boolean. So the actual returned value is the right value for && (considering both have to be truthy) and the first thuthy value found for || (considering only one need to be truthy). So this basically means: if a is a modular value (i.e. != 0) return a to add to the sum, else return 0.


            • , 0 is the initial value for the reduce calculation.


            Reduce documentation: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce



            Edit



            Answering to Tristan Forward:






            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);








            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            var divisors = n=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>s+(!(n % a) && a), 0);

            console.log(divisors(12));





            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);





            var divisorsList = ;
            var divisors = (n)=>[...Array(n+1).keys()].slice(1)
            .reduce((s, a)=>{
            var divisor = !(n % a) && a;
            if (divisor) divisorsList.push(divisor);
            return s+divisor;
            }, 0);


            console.log('Result:', divisors(12));
            console.log('Divisors:', divisorsList);






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 16:01

























            answered Mar 31 '17 at 22:58









            Nelson Teixeira

            3,64621742




            3,64621742








            • 3




              Incredible readability.
              – destoryer
              Mar 31 '17 at 23:25






            • 1




              I haven't finished yet! :D
              – Nelson Teixeira
              Mar 31 '17 at 23:27








            • 1




              Now it's finished! take a look :)
              – Nelson Teixeira
              Mar 31 '17 at 23:28








            • 2




              @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
              – Nelson Teixeira
              Apr 1 '17 at 16:01






            • 2




              Created an explanation of the code to make it better :)
              – Nelson Teixeira
              Apr 1 '17 at 16:27














            • 3




              Incredible readability.
              – destoryer
              Mar 31 '17 at 23:25






            • 1




              I haven't finished yet! :D
              – Nelson Teixeira
              Mar 31 '17 at 23:27








            • 1




              Now it's finished! take a look :)
              – Nelson Teixeira
              Mar 31 '17 at 23:28








            • 2




              @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
              – Nelson Teixeira
              Apr 1 '17 at 16:01






            • 2




              Created an explanation of the code to make it better :)
              – Nelson Teixeira
              Apr 1 '17 at 16:27








            3




            3




            Incredible readability.
            – destoryer
            Mar 31 '17 at 23:25




            Incredible readability.
            – destoryer
            Mar 31 '17 at 23:25




            1




            1




            I haven't finished yet! :D
            – Nelson Teixeira
            Mar 31 '17 at 23:27






            I haven't finished yet! :D
            – Nelson Teixeira
            Mar 31 '17 at 23:27






            1




            1




            Now it's finished! take a look :)
            – Nelson Teixeira
            Mar 31 '17 at 23:28






            Now it's finished! take a look :)
            – Nelson Teixeira
            Mar 31 '17 at 23:28






            2




            2




            @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
            – Nelson Teixeira
            Apr 1 '17 at 16:01




            @Kinduser these days I posted an answer which gave an answer that only changed a small problem to make a very bad begginer's code work.Someone posted a more advanced version of the code and criticized me for not making a better version of the code.I replyied saying people have to learn in steps. Yet I thought about that and I realise that the best would be having both answers. An answer that gives the fish, and the other that teaches how to fish. So, based on this I thought in providing the user with a more advanced code, so the OP can have more advanced examples to reach higher. :)
            – Nelson Teixeira
            Apr 1 '17 at 16:01




            2




            2




            Created an explanation of the code to make it better :)
            – Nelson Teixeira
            Apr 1 '17 at 16:27




            Created an explanation of the code to make it better :)
            – Nelson Teixeira
            Apr 1 '17 at 16:27












            up vote
            3
            down vote













            You have to check if specified number is or not a divisor of given integer. You can use modulo % - if there's no rest, specified number is the divisor of the given integer - add it to the sum.






            function sumDivisors(num){
            var sum = 0;
            for (var i = 1; i <= num; i++){
            if (!(num % i)) {
            sum += i;
            }
            }
            console.log(sum);
            }

            sumDivisors(6);
            sumDivisors(10);








            share|improve this answer

























              up vote
              3
              down vote













              You have to check if specified number is or not a divisor of given integer. You can use modulo % - if there's no rest, specified number is the divisor of the given integer - add it to the sum.






              function sumDivisors(num){
              var sum = 0;
              for (var i = 1; i <= num; i++){
              if (!(num % i)) {
              sum += i;
              }
              }
              console.log(sum);
              }

              sumDivisors(6);
              sumDivisors(10);








              share|improve this answer























                up vote
                3
                down vote










                up vote
                3
                down vote









                You have to check if specified number is or not a divisor of given integer. You can use modulo % - if there's no rest, specified number is the divisor of the given integer - add it to the sum.






                function sumDivisors(num){
                var sum = 0;
                for (var i = 1; i <= num; i++){
                if (!(num % i)) {
                sum += i;
                }
                }
                console.log(sum);
                }

                sumDivisors(6);
                sumDivisors(10);








                share|improve this answer












                You have to check if specified number is or not a divisor of given integer. You can use modulo % - if there's no rest, specified number is the divisor of the given integer - add it to the sum.






                function sumDivisors(num){
                var sum = 0;
                for (var i = 1; i <= num; i++){
                if (!(num % i)) {
                sum += i;
                }
                }
                console.log(sum);
                }

                sumDivisors(6);
                sumDivisors(10);








                function sumDivisors(num){
                var sum = 0;
                for (var i = 1; i <= num; i++){
                if (!(num % i)) {
                sum += i;
                }
                }
                console.log(sum);
                }

                sumDivisors(6);
                sumDivisors(10);





                function sumDivisors(num){
                var sum = 0;
                for (var i = 1; i <= num; i++){
                if (!(num % i)) {
                sum += i;
                }
                }
                console.log(sum);
                }

                sumDivisors(6);
                sumDivisors(10);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 31 '17 at 22:28









                kind user

                19.3k51841




                19.3k51841






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43150520%2fjavascript-find-the-sum-of-all-divisors-of-a-given-integer%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Sphinx de Gizeh

                    Dijon

                    Guerrita