Javascript implements a numeric addition Function

Source: Internet
Author: User

Javascript implements a numeric addition Function

This article mainly introduces how to implement a numeric addition function in javascript, which is very simple and practical for you to understand javascript. If you need it, you can refer to it.

If you don't talk nonsense, simply provide the code.

JS

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

<Script type = "text/javascript">

Function Sum (arg1, arg2) {// numeric addition function

Var sarg1 = new String (arg1); // convert the input parameter into a String for parameter check

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 (); // If a parameter is null, an exception is thrown.

E0.Serial = 1000001; // error code

If (sarg1 = "") // enter the correct error message based on the null Parameter

{

The e0.message = "Sum function parameter is invalid: the first parameter is null! "; // Error description

}

Else

{

The e0.message = "Sum function parameter is invalid: the first parameter is null! ";

}

Throw e0; // throw an error message

}

For (I = 0; I <sarg1.length; I ++) {// check the parameter Validity

For (j = 0; j <10; j ++) {// check all characters

If (sarg1.charAt (I) = j) // if it is not a number, an error message is thrown.

{

Break; // skip Loop

}

Else

{

If (j = 9) // when the number 9 has been queried

{

Var e1 = new Error (); // Error message object

E1.Serial = 1000001; // error code

E1.message = "Sum function parameter:" + sarg1 + "is an invalid number! "; // Error description

Throw e1;

}

}

}

}

For (k = 0; k <sarg2.length; k ++) // check that parameter 2 is a number

{

For (l = 0; l <10; l ++) {// compare one by one from 0 to 9

If (sarg2.charAt (k) = l) // if it is 0 ~ 9 digits

{

Break;

}

Else

{

If (l = 9) // only contains non-numbers, an error message is thrown.

{

Var e2 = new Error (); // create an Error object

E2.Serial = 1000001; // error code

E2.message = "Sum function parameter:" + sarg2 + "is an invalid number! ";

Throw e2;

}

}

}

}

Return Number (arg1) + Number (arg2); // if both parameters are correct, two values are returned.

}

Function Button1_onclick () {// single-host event handler for the "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); // call the function for Calculation

 

Text3.value = sum; // output the calculation result

}

Catch (e) {// output error message if an error occurs

Alert (e. message); // output information in the exception

If (e. Serail = 1000001) // if it is error 1000001

{

Alert (e. message );

E = null;

}

}

}

</Script>

HTML:

?

1

2

3

4

5

6

<Input type = "text" id = "Text1" style = "width: 84px" maxlength = "20"/>

+

<Input type = "text" id = "Text2" style = "width: 75px" maxlength = "20"/>

=

<Input type = "text" id = "Text3" style = "width: 69px">

<Input type = "button" id = "Button1" value = "Calculate" onclick = "return Button1_onclick ()"/>

The above is all the content of this article. I hope you will like it.

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.