Implements a numeric addition function that returns the sum of two digits. Parameter validation is required and throws an exception if the parameter is not a number or is empty.

Source: Internet
Author: User

<body>

<script type= "Text/javascript" >
function Sum (ARG1,ARG2) {//numeric addition functions
var sarg1 = new String (ARG1); Convert incoming arguments to strings for parameter checking
var sarg2 = new String (ARG2); Convert parameter 2 to character type
if (sarg1== "") | | (sarg2== "")) Make sure the parameter is not empty
{
var E0 = new Error (); Throws an exception when a parameter is empty
E0. Serial = 1000001; Error number
if (sarg1== "")//fill in the correct error message according to the null parameter
{
E0.message = "Sum function argument is illegal: the 1th parameter is empty! "; Error description Information
}
Else
{
E0.message = "Sum function argument is illegal: the 2nd parameter is empty! ";
}
throw E0; Throw an error message
}
for (i=0;i<sarg1.length;i++) {//parameter legality check
for (j=0;j<10;j++) {//Check all characters
if (Sarg1.charat (i) ==j)//If it is not a number, throw an error message
{
Break Jump out of the loop
}
Else
{
if (j==9)//When the number has been queried to 9 o'clock
{
var e1 = new Error (); Error Message Object
E1. Serial = 1000001; Error number
E1.message = "Sum function parameter:" + Sarg1 + "is an illegal number! "; Error description Information
Throw E1;
}
}
}
}
for (k=0;k<sarg2.length;k++)//Check parameter 2 is a number
{
for (l=0;l<10;l++) {//compare from 0 to 9 individually
if (Sarg2.charat (k) ==l)//If the number is 0~9
{
Break
}
Else
{
if (l==9)//only contains non-digits throws an error message
{
var e2 = new Error (); Create Error Object
E2. Serial = 1000001; Exception number
E2.message = "Sum function parameter:" + Sarg2 + "is an illegal number! ";
throw E2;
}
}
}
}
Return number (ARG1) + number (ARG2); Parameters are correct then return two worth of
}
Single-machine event handler for function Button1_onclick () {//"COMPUTE" button
try{
var Text1 = document.getElementById ("Text1");
var Text2 = document.getElementById ("Text2");
var Text3 = document.getElementById ("Text3");
var sum = SUM (text1.value,text2.value); Calling functions for calculations

Text3.value = sum; Output calculation results
}
catch (E) {//error message output error occurs
alert (e.message); Information in the output exception
if (E.serail = = 1000001)//If it is number 1,000,001th error
{
alert (e.message);
e = null;
}
}
}
</script>

<input type= "text" id= "Text1" style= "width:84px" maxlength= "/>"
+
<input type= "text" id= "Text2" style= "width:75px" maxlength= "/>"
=
<input type= "text" id= "Text3" style= "width:69px" >
<input type= "button" id= "Button1" value= "Calculate" onclick= "return Button1_onclick ()"/>
</body>

Implements a numeric addition function that returns the sum of two digits. Parameter validation is required and throws an exception if the parameter is not a number or is empty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.