minecraftModder
minecraftModder

Reputation: 13

NaN is clearly a number, or is it?

I'm having a bit of trouble here. I checked to make sure my variable was defined only with numbers, but the code is still returning as not a number. Anyone know what I am doing wrong?

Code below:

//declare vars and/or constants
var endString = "Total cost per night is $";
var viewCost;
var discount;

//input vars
pplCount = prompt("How many people will be staying?");
discYes = prompt("Do you have an AAA discount?");
viewYes = prompt("Do you want a room with a view?");

//if variables

if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "Yes") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = +roomCost - discount + viewCost;
viewCost = (+roomCost * 0.10);
document.write(endString + roomCost);
}else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "Yes") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost - discount + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "Yes") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost - discount + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "Yes") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "Yes") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "Yes") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost + viewCost;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "No" && viewYes === "No") {
discount = 50 * 0.15;
roomCost = 50;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "No" && viewYes === "No") {
discount = 60 * 0.10;
roomCost = 60;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "No" && viewYes === "No") {
discount = 70 * 0.05;
roomCost = 70;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

else if(pplCount === "1" || pplCount === "2" && discYes === "Yes" && viewYes === "No") {
discount = 50 * 0.15;
roomCost = 50;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "3" || pplCount === "4" && discYes === "Yes" && viewYes === "No") {
discount = 60 * 0.10;
roomCost = 60;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}
else if(pplCount === "5" || pplCount === "6" && discYes === "Yes" && viewYes === "No") {
discount = 70 * 0.05;
roomCost = 70;
roomCost = roomCost - discount;
viewCost = (roomCost * 0.10);
document.write(endString + roomCost);
}

Is it because I'm defining roomCost twice?

Upvotes: 0

Views: 180

Answers (3)

Halcyon
Halcyon

Reputation: 57709

NaN is what you get when a Number operation gives an invalid result (Not a Number)

Like:

5 - "a"
5 + NaN
parseInt("a", 10)
5 + undefined

NaN is poisonous in the sense that any operation that has NaN as an operand will return NaN as well. Figure out where you bad operation is.

Glancing at you're code, I see that you're operating with strings instead of Numbers. Use parseInt() to convert a string into a proper Number. Make sure you validate your input, if parseInt returns NaN give an error.

Upvotes: 3

svidgen
svidgen

Reputation: 14282

Ignoring the other naughty stuff you're doing here, you're using viewCost in a few places without defining it first. (It's Not A Number.)

Changing the first few lines to the following fixes the problem:

//declare vars and/or constants
var endString = "Total cost per night is $";
var viewCost = 0;
var discount = 0;

Upvotes: 2

Marc B
Marc B

Reputation: 360602

var viewCost; // <---defined, but not assigned to

roomCost = +roomCost - discount + viewCost;
                                  ^^^^^^^^

at the time you run this the first time, you've only declared the variable, but haven't assigned to it yet:

Upvotes: 1

Related Questions