nethken nethken - 3 months ago 14
Ajax Question

Getting an NaN error - Javascript

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;




}
});
}

Answer

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
}