JavaScript - Simple Basic Calculator

Submitted by: 
Language: 
Visitors have accessed this post 207 times.

In this tutorial we will create a Simple Basic Calculator using JavaScript. This code automatically generate a calculator app when the user run the web application. The code itself use onclick() function to initiate a several method by means of computing a number when you pass a value as parameter.. You can apply this script to your code, it is a user-friendly program feel free to modify it.

We will be using JavaScript as a server-side scripting language because It allows greater control of your web page behavior than HTML alone. It is embedded in HTML that responsible to allow user to interact with the computer.
.

Getting Started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this 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. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  11. </a> </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Simple Basic Calculator</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-6 well" style="background-color:green;">
  18. <form name="calculator">
  19. <input type="text" id="result" style="width:100%; height:50px; font-size:30px;">
  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 the Script

This code contains the script of the application. This code will calculate your equation when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory

  1. function calculate(val){
  2. document.getElementById("result").value+=val;
  3. }
  4.  
  5. function cancel(){
  6. document.getElementById("result").value='';
  7. }
  8.  
  9. function solve(){
  10. var number = document.getElementById("result").value;
  11. var total = eval(number);
  12. document.getElementById("result").value = total;
  13. }

There you have it we successfully created a Simple Basic 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.