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

How To Create Simple Scientific Calculator

Gconyi

Follower Engagement Pro
G Rep
0
0
0
Rep
0
G Vouches
0
0
0
Vouches
0
Posts
47
Likes
139
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Simple Scientific Calculator

In this project, we are going to learn on how to create a simple scientific calculator using JavaScript. This program has Clear, Cancel, Ok button and etc. Here's the script for a scientific calculator.

HTML Source Code

This is our source code for calculator.
  1. <form

    name

    =

    "scientific_calculator"

    >

  2. <h2

    >

    Scientific Calculator</

    h2

    >

  3. <table

    cellspacing

    =

    "0"

    cellpadding

    =

    "1"

    >
  4. <tr

    >
  5. <td

    colspan

    =

    "5"

    align

    =

    "center"

    ><input

    name

    =

    "display"

    class

    =

    "sc"

    value

    =

    "0"

    size

    =

    "44"

    maxlength

    =

    "25"

    ></

    td

    >
  6. </

    tr

    >
  7. <tr

    >
  8. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    "Clear"

    onclick

    =

    "this.form.display.value = 0 "

    ></

    td

    >
  9. <td

    align

    =

    "center"

    colspan

    =

    "2"

    ><input

    type

    =

    "button"

    value

    =

    " Cancel "

    onclick

    =

    "deleteChar(this.form.display)"

    ></

    td

    >
  10. <td

    align

    =

    "center"

    colspan

    =

    "2"

    ><input

    type

    =

    "button"

    value

    =

    " Ok "

    NAME

    =

    "Enter"

    onclick

    =

    "if (checkNum(this.form.display.value)) { compute(this.form) }"

    ></

    td

    >
  11. </

    tr

    >
  12. <tr

    >
  13. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " exp "

    onclick

    =

    "if (checkNum(this.form.display.value)) { exp(this.form) }"

    ></

    td

    >
  14. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 7 "

    onclick

    =

    "add_value(this.form.display, '7')"

    ></

    td

    >
  15. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 8 "

    onclick

    =

    "add_value(this.form.display, '8')"

    ></

    td

    >
  16. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 9 "

    onclick

    =

    "add_value(this.form.display, '9')"

    ></

    td

    >
  17. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " / "

    onclick

    =

    "add_value(this.form.display, '/')"

    ></

    td

    >
  18. </

    tr

    >
  19. <tr

    >
  20. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " ln "

    onclick

    =

    "if (checkNum(this.form.display.value)) { ln(this.form) }"

    ></

    td

    >
  21. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 4 "

    onclick

    =

    "add_value(this.form.display, '4')"

    ></

    td

    >
  22. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 5 "

    onclick

    =

    "add_value(this.form.display, '5')"

    ></

    td

    >
  23. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 6 "

    onclick

    =

    "add_value(this.form.display, '6')"

    ></

    td

    >
  24. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " * "

    onclick

    =

    "add_value(this.form.display, '*')"

    ></

    td

    >
  25. </

    tr

    >
  26. <tr

    >
  27. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " sqrt "

    onclick

    =

    "if (checkNum(this.form.display.value)) { sqrt(this.form) }"

    ></

    td

    >
  28. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 1 "

    onclick

    =

    "add_value(this.form.display, '1')"

    ></

    td

    >
  29. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 2 "

    onclick

    =

    "add_value(this.form.display, '2')"

    ></

    td

    >
  30. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 3 "

    onclick

    =

    "add_value(this.form.display, '3')"

    ></

    td

    >
  31. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " - "

    onclick

    =

    "add_value(this.form.display, '-')"

    ></

    td

    >
  32. </

    tr

    >
  33. <tr

    >
  34. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " sq "

    onclick

    =

    "if (checkNum(this.form.display.value)) { square(this.form) }"

    ></

    td

    >
  35. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " 0 "

    onclick

    =

    "add_value(this.form.display, '0')"

    ></

    td

    >
  36. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " . "

    onclick

    =

    "add_value(this.form.display, '.')"

    ></

    td

    >
  37. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " +/- "

    onclick

    =

    "changeSign(this.form.display)"

    ></

    td

    >
  38. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " + "

    onclick

    =

    "add_value(this.form.display, '+')"

    ></

    td

    >
  39. </

    tr

    >
  40. <tr

    >
  41. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " ( "

    onclick

    =

    "add_value(this.form.display, '(')"

    ></

    td

    >
  42. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    "cos"

    onclick

    =

    "if (checkNum(this.form.display.value)) { cos(this.form) }"

    ></

    td

    >
  43. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " sin "

    onclick

    =

    "if (checkNum(this.form.display.value)) { sin(this.form) }"

    ></

    td

    >
  44. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " tan"

    onclick

    =

    "if (checkNum(this.form.display.value)) { tan(this.form) }"

    ></

    td

    >
  45. <td

    align

    =

    "center"

    ><input

    type

    =

    "button"

    value

    =

    " ) "

    onclick

    =

    "add_value(this.form.display, ')')"

    ></

    td

    >
  46. </

    tr

    >
  47. </

    table

    >

  48. </

    form

    >

JavaScript

This script use to have a function in our calculator.
  1. <

    script>
  2. function

    add_value(

    input,

    character)

    {
  3. if

    (

    input.value

    ==

    null

    ||

    input.value

    ==

    "0"

    )
  4. input.value

    =

    character
  5. else
  6. input.value

    +=

    character
  7. }

  8. function

    cos(

    form)

    {
  9. form.display

    .value

    =

    Math

    .cos

    (

    form.display

    .value

    )

    ;
  10. }

  11. function

    sin(

    form)

    {
  12. form.display

    .value

    =

    Math

    .sin

    (

    form.display

    .value

    )

    ;
  13. }

  14. function

    tan(

    form)

    {
  15. form.display

    .value

    =

    Math

    .tan

    (

    form.display

    .value

    )

    ;
  16. }

  17. function

    sqrt(

    form)

    {
  18. form.display

    .value

    =

    Math

    .sqrt

    (

    form.display

    .value

    )

    ;
  19. }

  20. function

    ln(

    form)

    {
  21. form.display

    .value

    =

    Math

    .log

    (

    form.display

    .value

    )

    ;
  22. }

  23. function

    exp(

    form)

    {
  24. form.display

    .value

    =

    Math

    .exp

    (

    form.display

    .value

    )

    ;
  25. }

  26. function

    deleteChar(

    input)

    {
  27. input.value

    =

    input.value

    .substring

    (

    0

    ,

    input.value

    .length

    -

    1

    )
  28. }

  29. function

    changeSign(

    input)

    {
  30. if

    (

    input.value

    .substring

    (

    0

    ,

    1

    )

    ==

    "-"

    )
  31. input.value

    =

    input.value

    .substring

    (

    1

    ,

    input.value

    .length

    )
  32. else
  33. input.value

    =

    "-"

    +

    input.value
  34. }

  35. function

    compute(

    form)

    {
  36. form.display

    .value

    =

    eval(

    form.display

    .value

    )
  37. }

  38. function

    square(

    form)

    {
  39. form.display

    .value

    =

    eval(

    form.display

    .value

    )

    *

    eval(

    form.display

    .value

    )
  40. }

  41. function

    checkNum(

    str)

    {
  42. for

    (

    var

    i =

    0

    ;

    i <

    str.length

    ;

    i++

    )

    {
  43. var

    ch =

    str.substring

    (

    i,

    i+

    1

    )
  44. if

    (

    ch <

    "0"

    ||

    ch >

    "9"

    )

    {
  45. if

    (

    ch !=

    "/"

    &&

    ch !=

    "*"

    &&

    ch !=

    "+"

    &&

    ch !=

    "-"

    &&

    ch !=

    "."
  46. &&

    ch !=

    "("

    &&

    ch!=

    ")"

    )

    {
  47. alert(

    "invalid entry!"

    )
  48. return

    false
  49. }
  50. }
  51. }
  52. return

    true
  53. }
  54. </

    script>

And, this is the style of our calculator.
  1. <style type=

    "text/css"

    >
  2. form {
  3. width

    :

    440px

    ;
  4. padding

    :

    25px

    ;
  5. margin

    :

    auto

    ;
  6. border

    :

    5px

    solid

    blue

    ;
  7. border-radius

    :

    15px

    15px

    ;
  8. }

  9. input {
  10. background

    :

    azure

    ;
  11. color

    :

    blue

    ;
  12. font-size

    :

    15px

    ;
  13. padding-top

    :

    15px

    ;
  14. padding-bottom

    :

    15px

    ;
  15. padding-left

    :

    19px

    ;
  16. padding-right

    :

    19px

    ;
  17. margin

    :

    6px

    ;
  18. border

    :

    1px

    solid

    blue

    ;
  19. }

  20. h2 {
  21. text-align

    :

    center

    ;
  22. }

  23. h2 {
  24. color

    :

    blue

    ;
  25. }

  26. .sc

    {
  27. background

    :

    #fff

    ;
  28. color

    :

    #000

    ;
  29. }
  30. </style>

This is the result:
image.png


Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top