JavaScript - Simple Grade Calculator Using jQuery

Submitted by: 
Language: 
Visitors have accessed this post 198 times.

In this tutorial we will create a Simple Grade Calculator using jQuery. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is widely used in designing a stunning website. It is an interpreted programming language that has a capabilities of Object-Oriented. This code can be used as your calculator to any mathematical problem. So Let's do the coding...

Getting started:

This is the link for the bootstrap that has been used for the layout of the calculator 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="<a href="https://sourcecodester.com">Sourcecodester</a>
  10. " rel="nofollow">https://sourcecodester.com">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 - Simple Grade Calculator Using jQuery</h3>
  16. <hr style="border-top:1px dotted;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <div class="form-group" id="data">
  20. <input type="number" placeholder="Enter a number" class="form-control grade" min="0" max="100"/>
  21. </div>
  22. <div id="result">
  23. </div>
  24. <center><button class="btn btn-primary" id="calculate"><span class="glyphicon glyphicon-credit-card"></span> Calculate</button> <button class="btn btn-success" id="reset"><span class="glyphicon glyphicon-refresh"></span> Reset</button> <button class="btn btn-warning" id="add"><span class="glyphicon glyphicon-plus"></span> Add</button></center>
  25. </div>
  26. </div>
  27. <script src="js/jquery-3.2.1.min.js"></script>
  28. <script src="js/script.js"></script>
  29. </body>
  30. </html>

Creating the Script

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

There you have it we successfully created a Simple Grade Calculator 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!


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

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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.