How to Automatically Calculate Numbers in JavaScript

How to Automatically Calculate Numbers in JavaScript

Introduction

In this tutorial we will create a How to Create an Email Validation Form in JavaScript. This tutorial purpose is to teach you how to create a automatic calculation. This will cover all the important functionality that allow you to auto calculate the number. 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 add some auto calculation. I will give my best to provide you the easiest way of creating this program Auto Calculate Number. So let's do the coding.

Before we get started:

Here 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 display the html form and the result. 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. <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">How to Automatically Calculate Numbers</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-12">
  17. <div class="col-md-4">
  18. <label>First number</label>
  19. <div class="form-group">
  20. <input type="number"class="form-control" id="number1" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  21. </div>
  22. </div>
  23. <div class="col-md-4">
  24. <label>Arithmetic</label>
  25. <div class="form-group">
  26. <select id="symbol" class="form-control" onchange="autoCalculate()">
  27. <option value="add">+</option>
  28. <option value="sub">-</option>
  29. <option value="mult">x</option>
  30. <option value="div">/</option>
  31. </select>
  32. </div>
  33. </div>
  34. <div class="col-md-4">
  35. <label>Second Number</label>
  36. <div class="form-group">
  37. <input type="number"class="form-control" id="number2" onkeyup="autoCalculate()" placeholder="Enter a number"/>
  38. </div>
  39. </div>
  40. </div>
  41. <br />
  42. <div class="col-md-8">
  43. <div id="result"></div>
  44. </div>
  45. </div>
  46. <script src="script.js"></script>
  47. </body>
  48. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will auto calculate the two number you have entered. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. function autoCalculate(){
  2. let number1 = document.getElementById('number1').value;
  3. let number2 = document.getElementById('number2').value;
  4. let 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('result').innerHTML = "<h1>RESULT: "+"<span class='text-success' style='font-size:4rem'>"+total+"</span>"+"</h1>";
  12. break;
  13. case "sub":
  14. var digit1 = parseFloat(number1);
  15. var digit2 = parseFloat(number2);
  16. var total = digit1 - digit2;
  17. document.getElementById('result').innerHTML = "<h1>RESULT: "+"<span class='text-success' style='font-size:4rem'>"+total+"</span>"+"</h1>";
  18. break;
  19. case "mult":
  20. var digit1 = parseFloat(number1);
  21. var digit2 = parseFloat(number2);
  22. var total = digit1 * digit2;
  23. document.getElementById('result').innerHTML = "<h1>RESULT: "+"<span class='text-success' style='font-size:4rem'>"+total+"</span>"+"</h1>";
  24. break;
  25. case "div":
  26. var digit1 = parseFloat(number1);
  27. var digit2 = parseFloat(number2);
  28. var total = digit1 / digit2;
  29. document.getElementById('result').innerHTML = "<h1>RESULT: "+"<span class='text-success' style='font-size:4rem'>"+total+"</span>"+"</h1>";
  30. break;
  31. }
  32. }else{
  33. document.getElementById('result').innerHTML = '';
  34. }
  35. }

In the code provided we just only create one method called autoCalculate(). This function will auto calculate the number you have entered. This code uses switch cases to enable you to switches between function base on the parameter you set. To calculate the number we just simply put the arithmetic between the two numbers.

Output:

The How to Automatically Calculate Numbers in 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 Automatically Calculate Numbers in 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

Comments

Add new comment