JavaScript - Simple Auto Calculate Number

In this tutorial we will create a Simple Auto Calculate Number using JavaScript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

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. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  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 Auto Calculate</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-3"></div>
  17. <div class="col-md-6">
  18. <div class="form-group">
  19. <input type="number"class="form-control" id="number1" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  20. </div>
  21. <div class="form-group">
  22. <select id="symbol" class="form-control" onchange="autoCalculate()">
  23. <option value="add">+</option>
  24. <option value="sub">-</option>
  25. <option value="mult">x</option>
  26. <option value="div">/</option>
  27. </select>
  28. </div>
  29. <div class="form-group">
  30. <input type="number"class="form-control" id="number2" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  31. </div>
  32. <div class="form-group">
  33. <input type="text" class="form-control" id="answer" disabled="disabled"/>
  34. </div>
  35. </div>
  36. </div>
  37. <script src="js/script.js"></script>
  38. </body>
  39. </html>

Creating the Script

This code contains the script of the application. This code will calculate the number you have entered base on the equation you use. 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 folder.
  1. function autoCalculate(){
  2. var number1 = document.getElementById('number1').value;
  3. var number2 = document.getElementById('number2').value;
  4. var symbol = document.getElementById('symbol').value
  5. if(number1 != "" && number2 != ""){
  6. switch(symbol){
  7. case "add":
  8. var digit1 = parseFloat(number1);
  9. var digit2 = parseFloat(number2);
  10. var total = digit1 + digit2;
  11. document.getElementById('answer').value = total;
  12. break;
  13. case "sub":
  14. var digit1 = parseFloat(number1);
  15. var digit2 = parseFloat(number2);
  16. var total = digit1 - digit2;
  17. document.getElementById('answer').value = total;
  18. break;
  19. case "mult":
  20. var digit1 = parseFloat(number1);
  21. var digit2 = parseFloat(number2);
  22. var total = digit1 * digit2;
  23. document.getElementById('answer').value = total;
  24. break;
  25. case "div":
  26. var digit1 = parseFloat(number1);
  27. var digit2 = parseFloat(number2);
  28. var total = digit1 / digit2;
  29. document.getElementById('answer').value = total;
  30. break;
  31. }
  32. }else{
  33. document.getElementById('answer').value = '';
  34. }
  35. }
There you have it we successfully created a Simple Auto Calculate Number 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