-1

Im getting an NaN error in the total grade. Can someone help me to find the cause of this error? Or give me some ideas how to fix this? Im just new to javascript and web development. Sorry for the logic of my code..

enter image description here

here's my code.

              function Calculate(){
                var term = $("#term").val();
                var fac_code = $("#faculty_code").val();
            $.ajax({    
              type: 'POST',                                  
              url: 'getrecords.php',                        
              data: {
                "done": 1,
                "term": term,
                "fac_code": fac_code

              },                       
              dataType: 'json',                  
              success: function(data)         
              {
                var major = data[3];              
                var quizzes = data[4];    
                var homework = data[5];      
                var attendance = data[6]; 
                var laboratory = data[7];  
                var activity = data[8];  
                var recitation = data[9];


              var q = +(document.getElementById('quiz').textContent);
              var a = +(document.getElementById('atten').textContent);
              var h = +(document.getElementById('home').textContent);
              var r = +(document.getElementById('reci').textContent);
              var m = +(document.getElementById('me').textContent);
              var ac = +(document.getElementById('activityy').textContent);
              var l = +(document.getElementById('laboratory').textContent);

              var ma = $('.me');
              var qui = $('.quize');
              var homesea = $('.hos');
              var re = $('.recits');
              var laborat = $('.labo');
              var activit = $('.activity');
              var attenda = $('.atte');

              var MisVisible = ma.is(':visible');
              var QisVisible = qui.is(':visible');
              var HisVisible = homesea.is(':visible');
              var RisVisible = re.is(':visible');
              var LisVisible = laborat.is(':visible');
              var AisVisible = activit.is(':visible');
              var ATisVisible = attenda.is(':visible');

                var mt;
                var qt;
                var ht;
                var rt;
                var act;
                var lt;
                var att;

                if (MisVisible === true) {

                  mt = m / 100 * 50 + 50;

                } 
                if (QisVisible === true){

                 qt = q / 100 * 50 + 50;

                }
                if(HisVisible === true){

                 ht = h / 100 * 50 + 50;

                }
                if(RisVisible === true){

                  rt = r / 100 * 50 + 50;
                }
                if(LisVisible === true){

                  lt = l / 100 * 50 + 50;   
                }
                if(AisVisible === true){

                  act = ac / 100 * 50 + 50;

                }
                if(ATisVisible === true){

                    att = a / 100 * 50 + 50;
                }




                  mtt = mt * (major * 0.01);
                  qtt = qt * (quizzes * 0.01);
                  attt = att * (attendance * 0.01);
                  htt = ht * (homework * 0.01);
                  rtt = rt * (recitation * 0.01);
                  actt = act * (activity * 0.01);
                  ltt = lt * (laboratory * 0.01);

          var grade = mtt + qtt + attt + htt + rtt + actt + ltt;

          document.getElementById('td_grade').innerHTML = grade;




              } 
            });
        }
nethken
  • 1,072
  • 7
  • 24
  • 40
  • have a look at http://stackoverflow.com/questions/3215120/why-javascript-says-that-a-number-is-not-a-number – Manish Aug 25 '16 at 03:38
  • Javascript is type strict. Something from html can be a string. Use parseInt or parseFloat to ensure certain number. – daremachine Aug 25 '16 at 03:39
  • NaN means Not-A-Number, make sure to check all your variables if you are getting your desired values. You can use console.log(variable_name) to watch your variables. – jomsk1e Aug 25 '16 at 03:47
  • @jomsk1e where should i put that sir? – nethken Aug 25 '16 at 03:50
  • You can put this on any line to check the current value of your variable, example: mtt = mt * (major*0.01);console.log(mtt); And use chrome's dev tool by pressing f12 and click console tab. – jomsk1e Aug 25 '16 at 04:06
  • The mt is undefined.. – nethken Aug 25 '16 at 04:07

2 Answers2

2

The best answer would be to know how to use the Dev Tools source pane. Put breakpoints in your code, and start stepping through the code to see the values.

If I were to take a wild guess, one of these is undefined or missing.

var major = data[3];              
var quizzes = data[4];    
var homework = data[5];      
var attendance = data[6]; 
var laboratory = data[7];  
var activity = data[8];  
var recitation = data[9];

Or one of these can't be parsed correctly by the unary + as a number

var q = +(document.getElementById('quiz').textContent);
var a = +(document.getElementById('atten').textContent);
var h = +(document.getElementById('home').textContent);
var r = +(document.getElementById('reci').textContent);
var m = +(document.getElementById('me').textContent);
var ac = +(document.getElementById('activityy').textContent);
var l = +(document.getElementById('laboratory').textContent);
Joseph
  • 117,725
  • 30
  • 181
  • 234
1

Use Chrome's Dev tool by pressing F12 and go to console tab, to check for any variable you can use:

console.log(variable_name);

Additionally, your HTML value maybe not in a form of int so you should not use unary plus to get the value, use this instead to check whether your value is good:

if (!isNaN(parseFloat("10000")){
    //do something
}
jomsk1e
  • 3,585
  • 7
  • 34
  • 59
  • The problem is `if (MisVisible === true)` this statement. I changed it to 0 and the result is no `NaN`.. – nethken Aug 25 '16 at 04:26
  • In that case you can add else with that condition: else { mt = 0; } so that you are sure that mt will have a value when you use it with your calculation. – jomsk1e Aug 25 '16 at 04:48