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

How to Create a Simple Calculator in Web Browser using JavaScript

D4RKC40S

Performance Debugging Pro
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
126
Likes
87
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
How to Create a Simple Calculator in Web Browser using JavaScript

Introduction

In this tutorial we will create a How to Create a Simple Calculator in Web Browser using JavaScript. This tutorial purpose is to teach you how to create a basic calculator. This will cover all the basic function that will that let you use a calculator in a web browser. I will provide a sample program to show the actual coding of this tutorial.

This tutorial is simple and easy to understand just follow the instruction I provided and you can do it without a problem. This program can be use to any system or application if you want to use a calculate in your website. I will give my best to provide you the easiest way of creating this program Basic Calculator. So let's do the coding.

Before we get started:

This is the link for the template that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the interface of the calculator. To create this simply copy and write it into your text editor, then save it as index.html.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    >
  3. <head

    >
  4. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  5. <meta

    charset

    =

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  6. </

    head

    >
  7. <body

    >
  8. <nav

    class

    =

    "navbar navbar-default"

    >
  9. <div

    class

    =

    "container-fluid"

    >
  10. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  11. </

    div

    >
  12. </

    nav

    >
  13. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  14. <div

    class

    =

    "col-md-6 well"

    >
  15. <h3

    class

    =

    "text-primary"

    >

    How to Create a Simple Calculator in Web Browser</

    h3

    >
  16. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  17. <div

    class

    =

    "col-md-6 well"

    style

    =

    "background-color:gray; margin-left:150px;"

    >
  18. <form

    name

    =

    "calculator"

    >
  19. <input

    type

    =

    "text"

    id

    =

    "result"

    style

    =

    "width:100%; height:50px; font-size:30px;"

    disabled

    =

    "disabled"

    >
  20. <br

    /

    ><br

    /

    >
  21. <button

    type

    =

    "button"

    onclick

    =

    "calculate('1')"

    style

    =

    "padding:25px; margin-left:10px; font-size:14px;"

    >

    1</

    button

    >
  22. <button

    type

    =

    "button"

    onclick

    =

    "calculate('2')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    2</

    button

    >
  23. <button

    type

    =

    "button"

    onclick

    =

    "calculate('3')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    3</

    button

    >
  24. <button

    type

    =

    "button"

    onclick

    =

    "calculate('+')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    +</

    button

    >
  25. <br

    /

    ><br

    /

    >
  26. <button

    type

    =

    "button"

    onclick

    =

    "calculate('4')"

    style

    =

    "padding:25px; margin-left:10px; font-size:14px;"

    >

    4</

    button

    >
  27. <button

    type

    =

    "button"

    onclick

    =

    "calculate('5')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    5</

    button

    >
  28. <button

    type

    =

    "button"

    onclick

    =

    "calculate('6')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    6</

    button

    >
  29. <button

    type

    =

    "button"

    onclick

    =

    "calculate('-')"

    style

    =

    "padding-top:25px; padding-bottom:25px; padding-right:27px; padding-left:27px; font-size:14px;"

    >

    -</

    button

    >
  30. <br

    /

    ><br

    /

    >
  31. <button

    type

    =

    "button"

    onclick

    =

    "calculate('7')"

    style

    =

    "padding:25px; margin-left:10px; font-size:14px;"

    >

    7</

    button

    >
  32. <button

    type

    =

    "button"

    onclick

    =

    "calculate('8')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    8</

    button

    >
  33. <button

    type

    =

    "button"

    onclick

    =

    "calculate('9')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    9</

    button

    >
  34. <button

    type

    =

    "button"

    onclick

    =

    "calculate('*')"

    style

    =

    "padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:13px;"

    >

    x</

    button

    >
  35. <br

    /

    ><br

    /

    >
  36. <button

    type

    =

    "button"

    onclick

    =

    "cancel();"

    style

    =

    "padding:25px; margin-left:10px; font-size:14px;"

    >

    C</

    button

    >
  37. <button

    type

    =

    "button"

    onclick

    =

    "calculate('0')"

    style

    =

    "padding:25px; font-size:14px;"

    >

    0</

    button

    >
  38. <button

    type

    =

    "button"

    onclick

    =

    "solve()"

    style

    =

    "padding:25px; font-size:14px;"

    >

    =</

    button

    >
  39. <button

    type

    =

    "button"

    onclick

    =

    "calculate('/')"

    style

    =

    "padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:14px;"

    >

    /</

    button

    >
  40. </

    form

    >
  41. </

    div

    >
  42. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  43. </

    body

    >
  44. </

    html

    >

Creating JavaScript Function

This is where the main function of the application is. This code will display the basic functionality of a real calculator. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function

    calculate(

    val)

    {
  2. document.getElementById

    (

    "result"

    )

    .value

    +=

    val;
  3. }

  4. function

    cancel(

    )

    {
  5. document.getElementById

    (

    "result"

    )

    .value

    =

    ''

    ;
  6. }

  7. function

    solve(

    )

    {
  8. var

    number =

    document.getElementById

    (

    "result"

    )

    .value

    ;
  9. var

    total =

    eval(

    number)

    ;
  10. document.getElementById

    (

    "result"

    )

    .value

    =

    total;
  11. }

In the code above we just only create several method that will calculate the number we just entered. The function includes for getting the total value of the entered number such as (1+2, 5*9). All the buttons are already bind with a pre-coded script so that we will only getting the number every time you clicked the button.

Output:

How%20to%20Create%20a%20Simple%20Calculator%20in%20Web%20Browser%20using%20JavaScript%201.png


The How to Create a Simple Calculator in Web Browser using JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Create a Simple Calculator in Web Browser using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

More Tutorials for JavaScript Language

JavaScript Tutorials


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

452,496

328,880

328,888

Top