Javascript - Simple Calculator

In this tutorial we will create a Simple Calculator using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the bootstrap that has been used for the layout of the calculator https://getbootstrap.com/

The Main Function

This code contains the main function of the application. This code is the source code of the calculator that display the designed of the application. This will calculate the equation when the equal button is pressed. To create this just write these block of code inside the text editor.
  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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Javascript - Simple Calculator</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-2"></div>
  17. <div class="col-md-8">
  18. <div class="col-md-1"></div>
  19. <div class="col-md-10 well" style="background-color:#ccc;">
  20. <form name="calculator">
  21. <input type="text" name="result" style="width:100%; height:50px; font-size:30px;">
  22. <br /><br />
  23. <button type="button" onclick="calculator.result.value+='1'" style="padding:25px; margin-left:5px; font-size:14px;">1</button>
  24. <button type="button" onclick="calculator.result.value+='2'" style="padding:25px; font-size:14px;" >2</button>
  25. <button type="button" onclick="calculator.result.value+='3'" style="padding:25px; font-size:14px;">3</button>
  26. <button type="button" onclick="calculator.result.value+='+'" style="padding:25px; font-size:14px;">+</button>
  27. <br /><br />
  28. <button type="button" onclick="calculator.result.value+='4'" style="padding:25px; margin-left:5px; font-size:14px;">4</button>
  29. <button type="button" onclick="calculator.result.value+='5'" style="padding:25px; font-size:14px;">5</button>
  30. <button type="button" onclick="calculator.result.value+='6'" style="padding:25px; font-size:14px;">6</button>
  31. <button type="button" onclick="calculator.result.value+='-'" style="padding-top:25px; padding-bottom:25px; padding-right:27px; padding-left:27px; font-size:14px;">-</button>
  32. <br /><br />
  33. <button type="button" onclick="calculator.result.value+='7'" style="padding:25px; margin-left:5px; font-size:14px;">7</button>
  34. <button type="button" onclick="calculator.result.value+='8'" style="padding:25px; font-size:14px;">8</button>
  35. <button type="button" onclick="calculator.result.value+='9'" style="padding:25px; font-size:14px;">9</button>
  36. <button type="button" onclick="calculator.result.value+='*'" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:13px;">x</button>
  37. <br /><br />
  38. <button type="button" onclick="calculator.result.value =''" style="padding:25px; margin-left:5px; font-size:14px;">C</button>
  39. <button type="button" onclick="calculator.result.value+='0'" style="padding:25px; font-size:14px;">0</button>
  40. <button type="button" onclick="calculator.result.value=eval(calculator.result.value)" style="padding:25px; font-size:14px;">=</button>
  41. <button type="button" onclick="calculator.result.value+='/'" style="padding-top:25px; padding-bottom:25px; padding-right:26px; padding-left:26px; font-size:14px;">/</button>
  42. </form>
  43. </div>
  44. </div>
  45. </div>
  46. </body>
  47. </html>
There you have it we successfully created a Simple Calculator 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!!!

Add new comment