JavaScript implements a numerical addition function _javascript technique

Source: Internet
Author: User

No more nonsense, just give me the code.

Js

<script type= "Text/javascript" > Function Sum (arg1,arg2) {//numeric addition function var sarg1 = new String (arg1);/////Convert incoming arguments to strings for argument Number check var sarg2 = new String (ARG2); Convert parameter 2 to character type if ((sarg1== "") | | (sarg2== "")) serial = 1000001; "; Error description Information} else {e0.message = "sum function argument illegal: 2nd argument is empty!"
";
} throw E0; Throw 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 not the number, throw the error message {break;//Jump Out of loop} else {if (j==9)//When query has been made to the number 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 numeric {for (l=0;l<10;l++) {//from 0 to 9 per comparison if (Sarg2.charat (k) ==l)//If the number of 0~9
{break;} else {if (l==9)///only contains non-numeric 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; }}}Number (ARG1) + number (ARG2); Parameters are correct returns the two-worth and} function Button1_onclick () {//"COMPUTE" button's stand-alone event handler try{var Text1 = document.getElementById ("Text1"); var Te
XT2 = document.getElementById ("Text2");
var Text3 = document.getElementById ("Text3"); var sum = SUM (text1.value,text2.value); Call function to compute text3.value = SUM; Output calculation result} catch (e) {//error occurred output error message alert (e.message);//Output exception information if (E.serail = 1000001)///If 1,000,001th error {alert (E.MESSAG 
e);
e = null;

 }} </script>

Html:

<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= "calculation" onclick= "return Button1_onclick ()"/>

The above mentioned is the entire content of this article, I hope you can enjoy.

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.