• We just launched and are currently in beta. Join us as we build and grow the community.

Simple Math Calculator Using JavaScript

ParziVaal

DevOps Ninja
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
180
Likes
86
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
We all know that calculator is very useful in terms of using it in our daily lives. In this article, I created a Simple Math Calculator Using JavaScript. What this the program does?

Is that we have two (2) text box for the user to input the two (2) value of number then the simple program will do the sum, difference, product, and quotient of the two (2) value of number input by the user after clicking the OK button.

HTML Tag

This is the HTML tag where the two (2) value number is needed to input.

  1. <center

    >
  2. <h3

    >

    Simple Math Calculator Using JavaScript</

    h3

    >
  3. <br

    >
  4. <div

    class

    =

    "text"

    >
  5. <label

    for

    =

    "valueNumber1"

    >

    Value No. 1</

    label

    >
  6. <input

    type

    =

    "number"

    autofocus=

    "autofocus"

    step=

    "1"

    min=

    "0"

    name

    =

    "valueNumber1"

    id

    =

    "valueNumber1"

    /

    >
  7. </

    div

    >
  8. <div

    class

    =

    "text"

    >
  9. <label

    for

    =

    "valueNumber2"

    >

    Value No. 2</

    label

    >
  10. <input

    type

    =

    "number"

    autofocus=

    "autofocus"

    step=

    "1"

    min=

    "0"

    name

    =

    "valueNumber2"

    id

    =

    "valueNumber2"

    /

    >
  11. </

    div

    >
  12. <button

    onclick

    =

    "computeAll()"

    style

    =

    "font-size:18px; background:azure; color:red; margin-right:100px;"

    title

    =

    "Click here to find the result."

    >

    Enter </

    button

    >
  13. <button

    onclick

    =

    "clearAll_Input()"

    style

    =

    "font-size:18px; background:azure; color:red;"

    title

    =

    "Click here to clear the text fields."

    >

    Reset </

    button

    >
  14. <br

    /

    >
  15. <br

    /

    >
  16. <br

    /

    >

  17. <p

    id

    =

    "display1"

    >

    </

    p

    >
  18. <p

    id

    =

    "display2"

    >

    </

    p

    >
  19. <p

    id

    =

    "display3"

    >

    </

    p

    >
  20. <p

    id

    =

    "display4"

    >

    </

    p

    >
  21. </

    center

    >

JavaScript

This is the script to work the function and to get the sum, difference, product and quotient of two (2) value numbers.

  1. <

    script>
  2. function

    computeAll(

    )
  3. {
  4. var

    valueNumber1 =

    document.getElementById

    (

    'valueNumber1'

    )

    .value

    ;
  5. var

    valueNumber2 =

    document.getElementById

    (

    'valueNumber2'

    )

    .value

    ;
  6. var

    sum =

    parseInt(

    valueNumber1)

    +

    parseInt(

    valueNumber2)

    ;
  7. var

    subtract =

    parseInt(

    valueNumber1)

    -

    parseInt(

    valueNumber2)

    ;
  8. var

    product =

    parseInt(

    valueNumber1)

    *

    parseInt(

    valueNumber2)

    ;
  9. var

    quotient =

    parseInt(

    valueNumber1)

    /

    parseInt(

    valueNumber2)

    ;
  10. document.getElementById

    (

    "display1"

    )

    .innerHTML

    =

    "The sum of "

    +

    valueNumber1 +

    " and "

    +

    valueNumber2 +

    " is "

    +

    sum +

    "."

    ;
  11. document.getElementById

    (

    "display2"

    )

    .innerHTML

    =

    "The difference between "

    +

    valueNumber1 +

    " and "

    +

    valueNumber2 +

    " is "

    +

    subtract +

    "."

    ;
  12. document.getElementById

    (

    "display3"

    )

    .innerHTML

    =

    "The product between "

    +

    valueNumber1 +

    " and "

    +

    valueNumber2 +

    " is "

    +

    product +

    "."

    ;
  13. document.getElementById

    (

    "display4"

    )

    .innerHTML

    =

    "The quotient between "

    +

    valueNumber1 +

    " and "

    +

    valueNumber2 +

    " is "

    +

    quotient +

    "."

    ;
  14. }
  15. function

    clearAll_Input(

    )
  16. {
  17. document.getElementById

    (

    "display1"

    )

    .innerHTML

    =

    ""

    ;
  18. document.getElementById

    (

    "display2"

    )

    .innerHTML

    =

    ""

    ;
  19. document.getElementById

    (

    "display3"

    )

    .innerHTML

    =

    ""

    ;
  20. document.getElementById

    (

    "display4"

    )

    .innerHTML

    =

    ""

    ;
  21. document.getElementById

    (

    "valueNumber1"

    )

    .value

    =

    ""

    ;
  22. document.getElementById

    (

    "valueNumber2"

    )

    .value

    =

    ""

    ;
  23. document.getElementById

    (

    "valueNumber1"

    )

    .focus

    (

    )

    ;
  24. }
  25. </

    script>

CSS

And, this is the style.

  1. <style type=

    "text/css"

    >
  2. p {
  3. font-family

    :

    monospace

    ;
  4. font-size

    :

    18px

    ;
  5. font-weight

    :

    bold

    ;
  6. color

    :

    red

    ;
  7. }

  8. div.text

    {
  9. margin

    :

    0

    ;
  10. padding

    :

    0

    ;
  11. padding-bottom

    :

    1.25em

    ;
  12. }

  13. div.text

    label {
  14. margin

    :

    0

    ;
  15. padding

    :

    0

    ;
  16. display

    :

    block

    ;
  17. font-size

    :

    100%

    ;
  18. width

    :

    8em

    ;
  19. font-size

    :

    18px

    ;
  20. font-weight

    :

    bold

    ;
  21. color

    :

    red

    ;
  22. }

  23. div.text

    input,
  24. div.text

    textarea {
  25. margin

    :

    0

    ;
  26. padding

    :

    0

    ;
  27. display

    :

    block

    ;
  28. font-size

    :

    100%

    ;
  29. }

  30. input:

    active

    ,
  31. input:

    focus

    ,
  32. input:

    hover

    ,
  33. textarea:

    active

    ,
  34. textarea:

    focus

    ,
  35. textarea:

    hover

    {
  36. background-color

    :

    aliceblue

    ;
  37. border-color

    :

    blue

    ;
  38. }

  39. h3 {
  40. color

    :

    red

    ;
  41. }

  42. #valueNumber1

    {
  43. font-size

    :

    18px

    ;
  44. text-align

    :

    center

    ;
  45. width

    :

    100px

    ;
  46. }

  47. #valueNumber2

    {
  48. font-size

    :

    18px

    ;
  49. text-align

    :

    center

    ;
  50. width

    :

    100px

    ;
  51. }
  52. </style>

So what can you say about this work? Share your thoughts in the comment section below or email me at [email protected]. Practice Coding. Thank you very much.

 

452,292

323,341

323,350

Top