Super Web Mart - Discount Coupons & Deals - Discount Promos, Coupon Codes, Discount deals and web site reviews
 

Author Topic: Add Sum by javascript  (Read 692 times)

0 Members and 1 Guest are viewing this topic.

Offline Solushunz

  • Full Member
  • ***
  • Posts: 130
    • View Profile
Add Sum by javascript
« on: August 09, 2011, 11:25:09 PM »
this is for sum the two number, i have select from a website, did not find where to put it

on my website.

Code: [Select]
<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)
     }
     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>
« Last Edit: August 10, 2011, 02:27:00 AM by seochapter »

Webmaster Forum

Add Sum by javascript
« on: August 09, 2011, 11:25:09 PM »

Offline jbard

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Web Partner Group
Re: Add Sum by javascript
« Reply #1 on: December 27, 2011, 09:55:30 PM »
You are trying to pass a character string and not a numeric value. The use of 'cm' forces the values to character string. If you need to include the measurement unit, you will need to perform a lot more work to identify the measurement unit, convert them all to a common unit, and convert the string to a numeric value. You should only be entering numbers.

Webmaster Forum

Re: Add Sum by javascript
« Reply #1 on: December 27, 2011, 09:55:30 PM »