0

This is my function which works if i pass parameters !

function getGrade(id, marks) {
      if( id === 1 && !isNaN(marks)){
        console.log("id, marks: "+ id + " " + marks);
        var grades = 0;
        if (marks <101 && marks > 79 ){
           grades = 4.0;
          }
        else if(marks < 80 && marks >69){
           grades = 3.5;
        }
        else {
           grades = 0;
        }
       }
       alert("grade: " + grades);
       return grades;
    }

Using jquery each loop to calculate result and getting grade undefined

$(".units").each(function() {
      var $this = $(this);
  
      if(!isNaN($this.val()) ) {
        marks = parseFloat($this.parent().find('.grade-select').val() || 0);
        console.log("id, marks:  " + id + " " + marks );
        var grade = getGrade(id, marks);
        console.log("grade: " + grade);
        point += parseFloat($this.val() || 0) * grade;
        credits += parseFloat($this.val() || 0)
      }
    });
    return  (point/credits).toFixed(2);
  }

chrome debug console

Hasan Ahamed
  • 186
  • 3
  • 6
  • 2
    Is jQuery's `each` supposed to return anything? [The docs](https://api.jquery.com/each/#each-function) do not mention a return value. – evolutionxbox Jul 30 '20 at 18:14
  • You cannot return anything from an `each()` call. In this case it may make more sense to generate an array of the values using `map()` – Rory McCrossan Jul 30 '20 at 18:18
  • It is not recognizing getGrade as a function. Otherwise, it should print the "Id, Marks..." twice. Are they on the same JS file? What order? – Sudip Shrestha Jul 30 '20 at 18:23
  • Does this answer your question? [Jquery each - Stop loop and return object](https://stackoverflow.com/questions/8224375/jquery-each-stop-loop-and-return-object) – bro Jul 30 '20 at 18:26

0 Answers0