Demo entry 2623743

adfafd

   

Submitted by anonymous on Sep 04, 2015 at 00:16
Language: JavaScript. Code size: 339 Bytes.

function sumDigits(num) {
     var i, sum = 0;                  // can declare two variables at once

     for (i = 1; i <= num; i++) {
             sum += i;              // add each number to sum (ie, 1 + 2 + ...+ num)
     }

     // Display result
     alert("The sum of the digits from 1 to "+ num + " is:\n\n\t " + sum);
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).