Skip to main content

Calculate Average Number Automatically Using JavaScript


In this code we will try to create Calculate Average Number Automatically using JavaScript. The program will calculate the entered number and display the average. The trick of this code is to get the sum of all numbers and divide the numbers base on total number. To learn more about this, just follow the steps below.

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. <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="navabar 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">Calculate Average Number Automatically Using JavaScript</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-4">
  17. <div class="form-group">
  18. <label>Grade #1</label>
  19. <input type="number" id="grade1" onkeyup="getAverage();" class="form-control"/>
  20. </div>
  21. <div class="form-group">
  22. <label>Grade #2</label>
  23. <input type="number" id="grade2" onkeyup="getAverage();" class="form-control"/>
  24. </div>
  25. <div class="form-group">
  26. <label>Grade #3</label>
  27. <input type="number" id="grade3" onkeyup="getAverage();" class="form-control"/>
  28. </div>
  29. <div class="form-group">
  30. <label>Grade #4</label>
  31. <input type="number" id="grade4" onkeyup="getAverage();" class="form-control"/>
  32. </div>
  33. <div class="form-inline">
  34. <label>Average Number</label>
  35. <input type="text" class="form-control" id="result" readonly="readonly"/>
  36. </div>
  37. </div>
  38. </div>
  39. <script src="js/script.js"></script>
  40. </body>
  41. </html>

Creating the Script

This code contains the script of the application. The code can auto calculate the total value of the input forms. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.

  1. function getAverage(){
  2. var grade1=document.getElementById('grade1').value;
  3. var grade2=document.getElementById('grade2').value;
  4. var grade3=document.getElementById('grade3').value;
  5. var grade4=document.getElementById('grade4').value;
  6. var total;
  7. var ave;
  8.  
  9. if(grade1 !="" && grade2 !="" && grade3 !="" && grade4 !=""){
  10. total=parseInt(grade1)+parseInt(grade2)+parseInt(grade3)+parseInt(grade4);
  11. ave=total/4
  12.  
  13. document.getElementById('result').value=ave.toFixed(0);
  14. }
  15.  
  16. }

There you have it we successfully created a Calculate Average Number Automatically 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.