Question

I have defined the following variable:

var TuesdaysRatio = myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal;

The console.log(TuesdaysRatio); is has the output in Chrome: 0.032636762568610575 . The next line in my script is the following:

$('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');

I am using JQuery to display the variable in a paragraph into an empty div (with the class ratio-div) created above the script tag. On my page instead of Tuesdays Likes per Organic Impressions Ratio 0.032636762568610575 I have Tuesdays Likes per Organic Impressions Ratio NaN and I can't figure it out where is the issue...

Was it helpful?

Solution

Number it.. Use Number() .. parseInt and parseFloat may change your result so go for Number() function which converts the object argument to a number that represents the object's value.

var TuesdaysRatio = Number(myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal);

OTHER TIPS

Try this

$(document).ready(function(){
     var TuesdaysRatio = parseFloat(parseInt(myTuesdaysNewLikesTotal) / parseInt(myTuesdaysImpressionsOrganicTotal));
     $('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});

Try to parse your value first:

var TuesdaysRatio = parseInt((myTuesdaysNewLikesTotal / myTuesdaysImpressionsOrganicTotal), 10);
$('document').ready(function(){
    var TuesdaysRatio = Number(0.032636762568610575);
    $('div.ratio-div').html('<p>Tuesdays Likes per Organic Impressions Ratio ' + TuesdaysRatio + '</p>');
});

Try this

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top