-2
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.

Migol
  • 8,161
  • 8
  • 47
  • 69

4 Answers4

3

Use the isNaN function for the comparison:

if(isNaN(mean)){
    alert("Error! Please use NUMBERS only");
}
Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

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.

Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187
0

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

Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278
0

You are assgining a string "NaN" to mean, not comparison.

Also you can't use mean === NaN to check, because compare to NaN always return false.

To check whether is a NaN, you have to use isNaN function.

if (isNaN(mean)) {
    alert("Error! Please use NUMBERS only");
}
xdazz
  • 158,678
  • 38
  • 247
  • 274