First I test that every variable got a number value:
09-11 18:15:00.420:
d_drop: -1.178791867393647
drop_at_zero: 0.0731037475605623
sightHeight: 4.5
d_distance: 40
zeroRange: 10
09-11 18:15:00.420:
d_drop: true
drop_at_zero: true
sightHeight: true
d_distance: true
zeroRange: true
function isNumber (o) {
return ! isNaN (o-0) && o != null;
}
var d_drop; // in calculation this gets value 1.1789
var d_path = -d_drop - sightHeight + (drop_at_zero + sightHeight) * d_distance / zeroRange;
console.log("Path: " + d_path + " cm");
and in the log:
09-11 18:15:00.430: D/CordovaLog(1533): Path: NaN cm
WHY? I have tried to figure that out couple of hours now and no success, maybe someone has an idea, I haven’t!
Thanks!
Sami
——-ANSWER IS that parse every variable when using + operand———–
var d_path = parseFloat(-d_drop) – parseFloat(sightHeight) + (parseFloat(drop_at_zero) + parseFloat(sightHeight)) * parseFloat(d_distance) / parseFloat(zeroRange);
The addition operator
+will cast things as strings if either operand is a string. You need to parse ALL of your inputs (d_drop,sightHeight, etc) as numbers before working with them.Here’s a demo of how the
+overload works. Notice how the subtraction operator-is not overloaded and will always cast the operands to numbers:http://jsfiddle.net/jbabey/abwhd/