set("mean",total/weight);
if (mean = "NaN") {
alert("Error! Please use NUMBERS only");
}
i want the alert to only run when the result is NaN, the calculation works without the if statement so i know that is not the problem.
set("mean",total/weight);
if (mean = "NaN") {
alert("Error! Please use NUMBERS only");
}
i want the alert to only run when the result is NaN, the calculation works without the if statement so i know that is not the problem.
Use the isNaN function for the comparison:
if(isNaN(mean)){
alert("Error! Please use NUMBERS only");
}
In JavaScript, mean = "NaN"
, with a single equals sets the value of mean
. The if statement will then check the "trueness" of the value "NaN" which will evaluate to true. That is, your if(mean = "NaN")
will first set mean
to the literal string "NaN"
, and then test if("NaN")
.
Use a double equals (mean == "NaN"
) for equality comparison, and triple equals (mean === "NaN"
) for type and equality comparison.
However, mean == "NaN"
will always return false
because NaN
is not the literal string "NaN"
but a special value. Use isNaN(mean)
instead.
Change
if (mean = "NaN") {
to
if (mean == "NaN") {
in the first u set a value into a var, in the second u do a logical comparison