Reputation: 4118
I am encountering a strange unwanted Number
to NaN
conversion, without knowing where and why. I am clearly missing something here but I cannot figure out what.
Any suggestion is most welcome ! ;)
Here is the javascript code:
updatedAbsoluteResult = currentlyDisplayedRentability * investment
resultAgainstReferencial = updatedAbsoluteResult - appropriateReferencialRentability * investment
$('#a0').html('currentlyDisplayedRentability: ' + typeof currentlyDisplayedRentability)
$('#a1').html('investment: ' + typeof investment)
$('#a2').html('updatedAbsoluteResult: ' + typeof updatedAbsoluteResult)
$('#a3').html('appropriateReferencialRentability: ' + typeof appropriateReferencialRentability)
$('#a4').html('resultAgainstReferencial: ' + typeof resultAgainstReferencial )
$('#a5').html('resultAgainstReferencial: ' + resultAgainstReferencial )
Here is the HTML output:
currentlyDisplayedRentability: number
investment: number
updatedAbsoluteResult: number
appropriateReferencialRentability: number
resultAgainstReferencial: number
resultAgainstReferencial: NaN
Everything is of Number type, but when I want to return the final result, I get 'is not a number' as a result. Anyone knows why ?
Enjoy your weekends !
Upvotes: 0
Views: 371
Reputation: 707746
The problem is likely that resultAgainstReferencial is NaN
. You are getting the results you are because:
typeof NaN == "number"
To show you:
resultAgainstReferencial = NaN;
alert(typeof resultAgainstReferencial); // alerts "number"
You can even see it here: http://jsfiddle.net/jfriend00/t3ubg/
So, somewhere upstream, you're trying to do math with things that aren't numbers. You'll have to look at the values of all the numeric inputs and see where the data has gone awry.
Upvotes: 1
Reputation: 91557
Perhaps ironically, NaN
is itself a Number
.
You may have a failed parseInt()
call somewhere, or numerous other possibilities. It's hard to guess without seeing the relevant code.
Upvotes: 0
Reputation: 14421
NaN
in javascript is technically still of type number
. It is usually caused from a division by 0. Doing any mathmatical calculations on another NaN
will also return NaN
.
Upvotes: 0
Reputation: 3021
A numeric type can return NaN and still be of numeric type. Check the values of updatedAbsoluteResult, appropriateReferencialRentability, and investment to see if any of those are unexpected values. Likely something in that equation has gone awry.
Upvotes: 2