JavaScript - Calculate Average Using jQuery

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 https://getbootstrap.com/. And this is the link for the jquery that i used in this tutorial https://jquery.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">JavaScript - Calculate Average Using jQuery</h3>
  15. <hr style="border-top:1px dotted;"/>
  16. <div class="col-md-3">
  17. <center><button class="btn btn-primary" id="getAve">Get Average</button></center>
  18. <br />
  19. <center><button class="btn btn-success" id="reset">Reset</button></center>
  20. <br />
  21. <center><button class="btn btn-warning" id="addField">Add Field</button></center>
  22. </div>
  23. <div class="col-md-6">
  24. <div class="form-group" id="data">
  25. <input type="number" placeholder="Enter a number" class="form-control grade" min="0" max="100"/>
  26. </div>
  27. <div id="result">
  28. </div>
  29. </div>
  30. </div>
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="js/script.js"></script>
  33. </body>
  34. </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. }
  9.  
  10. grades.push($(this).val());
  11. });
  12.  
  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. });
  20.  
  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. });
  25.  
  26. $('#reset').on('click', function(){
  27. window.location = "index.html";
  28. });
  29.  
  30.  
  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!

Comments

Submitted byGyze (not verified)on Sat, 12/18/2021 - 04:14

Sure

Add new comment