|
Post by seowebtrick on Aug 1, 2011 23:01:00 GMT -5
I have put that code for sum of numbers but i take a long time to add them.. is there any modifivation in this code.. please help me out of it. <SCRIPT> 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); }
</SCRIPT>
<BODY>
Looping Functions - Calculate the sum of the digits.
<FORM NAME="SumNums"> The sum of the digits from 1 to: <INPUT TYPE="text" NAME="charNum"> <INPUT TYPE="button" VALUE="Calculate" onClick="sumDigits(SumNums.charNum.value)"> </FORM> Web designer india, website designer india, Web design india,
|
|