JavaScript - Calculate Average Using jQuery

Submitted by: 
Visitors have accessed this post 152 times.

In this tutorial we will create a Calculate Average using jQuery. This code will dynamically calculate the average of a given number when the user click the get average button. The code itself use jQuery onclick function to generate new input field using appendTo() a jQuery built-in function that will append all the elements as a child of the parent element and use a binded id to calculate a given number with a special formula. This is a user-friendly program feel free to modify it.

We will be using jQuery a JavaScript framework that design to simplify HTML DOM tree traversal and manipulation. It can do more in a single line of code than JavaScript can do in a whole function.

Getting Started:

This is the link for the bootstrap that i used for the layout design

And this is the link for the jquery that i used in this tutorial

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="<a href="">Sourcecodester</a>
  10. " rel="nofollow">">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 - Calculate Average Using jQuery</h3>
  16. <hr style="border-top:1px dotted;"/>
  17. <div class="col-md-3">
  18. <center><button class="btn btn-primary" id="getAve">Get Average</button></center>
  19. <br />
  20. <center><button class="btn btn-success" id="reset">Reset</button></center>
  21. <br />
  22. <center><button class="btn btn-warning" id="addField">Add Field</button></center>
  23. </div>
  24. <div class="col-md-6">
  25. <div class="form-group" id="data">
  26. <input type="number" placeholder="Enter a number" class="form-control grade" min="0" max="100"/>
  27. </div>
  28. <div id="result">
  29. </div>
  30. </div>
  31. </div>
  32. <script src="js/jquery-3.2.1.min.js"></script>
  33. <script src="js/script.js"></script>
  34. </body>
  35. </html>

Creating the Script

This code contains the script of the application. This code will get the average of numbers 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. $(document).ready(function(){
  2. $('#getAve').on('click', function(){
  3. var grades = [];
  4. $('.grade').each(function(){
  5. if($(this).val() == ""){
  6. alert("Please complete the required field");
  7. return false;
  8. }
  10. grades.push($(this).val());
  11. });
  13. var sum = eval(grades.join('+')), avg = sum / grades.length;
  14. if(avg >= 75){
  15. $('#result').html("<center>The average is <label class='text-primary'>"+ avg.toFixed(0) +", </label> <label class='text-success'>You passed!</label></center>");
  16. }else if(avg < 75){
  17. $('#result').html("<center>The average is <label class='text-primary'>"+ avg.toFixed(0) +", </label> <label class='text-danger'>You failed!</label></center>");
  18. }
  19. });
  21. $('#addField').on('click', function(){
  22. var newField = $('<br /><input type="number" placeholder="Enter a number" class="form-control grade" min="0" max="100"/>');
  23. newField.appendTo($('#data'));
  24. });
  26. $('#reset').on('click', function(){
  27. window.location = "index.html";
  28. });
  31. });

There you have it we successfully created a Calculate Average using jQuery. 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.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.