Reputation: 2039
I am currently taking an intro to coding class and we are working with JS. My code is good but I am missing something because for the discount outputs I always get NAN. Does anyone know why this is happening?
//Input
var orderAmount = prompt("What is the order amount?");
var contractor = prompt("Are you a contractor? yes/no?");
var employee = prompt("Are you an employee? yes/no?");
var age = prompt("How old are you?");
//Constant
var employeeDisc = .10;
var largeOrderDisc = .05;
var contractorDisc = .20;
var taxRate = .08;
var noTax = 0;
//Calculations
if (orderAmount >= 800) {
var discounta = orderAmount * largeOrderdisc;
}else {
var discounta = 0;
}
if (contractor == "yes") {
var discountc = orderAmount * contractorDisc;
}else if(contractor == "no") {
var discountc = 0;
}
if(employee == "yes") {
var discounte = orderAmount * employeeDisc;
}else if(emplyee == "no") {
var discounte = 0;
}
var discount = discountc + discounte + discounta;
var subtotal = orderAmount - discount;
if (age >= 90){
tax = subtotal * noTax;
}else {
tax = subtotal * taxRate;
}
total = subtotal - tax;
//Output
document.write("Original Price: $" + orderAmount);
document.write("Discount: $" + discount);
document.write("Subtotal: $" + orderAmount);
document.write("Tax: $" + tax);
document.write("Final Price: $" + total);
document.write("Final Price: $" + total);
Sorry about the code not compiling. It is now fixed. The issue now is that my document.write are not writing.
Upvotes: 0
Views: 313
Reputation: 2115
Your else statements are now improper so the code won't run at all. Remove the expressions like (contractor == "no")
Here is a fiddle to show it working. http://jsfiddle.net/bitfiddler/6fYvd/
Upvotes: 1
Reputation: 12234
You are attempting to perform arithmetic calculations and comparisons using strings. NaN
is "Not A Number", which is the numeric result of an arithmetic operation that fails (for example dividing by zero, or any calculation on NaN).
Note the use of numbers, not strings, below:
var employeeDisc = .10;
var largeOrderDisc = .05;
var contractorDisc = .20;
var taxRate = .08;
var noTax = 0;
//Calculations
if (orderAmount >= 800) {
var discounta = orderAmount * largeOrderdisc;
} else {
var discounta = 0;
}
prompt()
will return a string. You should convert that to a number before performing your calculations. You probably want to use parseInt()
or parseFloat()
.
Here is a simple example which produces NaN
:
var x = 'x5';
var y = '2';
var difference = x - y;
console.log( difference ); // Note: you can use console.log() to write messages to the console in your browser's developer tools. It is handy for debugging.
Upvotes: 2
Reputation: 14937
While you really should use numeric variables, JavaScript will convert the values as needed. I tested your code and the real problem with it is these two lines:
}else (contractor == "no") {
and
}else (emplyee == "no") {
These should be
}else if(contractor == "no") {
and
}else if(emplyee == "no") {
The script won't even compile as posted, so I don't know how you're even getting NaN.
Upvotes: 1
Reputation: 854
It's because you declare your discount variables in if clauses. Define them outside of if clauses, then you will have no problem.
var discounta;
if (a > b) {
discounta = 0.1;
}
else {
discounta = 0.2;
}
Also check variable scopes for javascript.
Upvotes: 0
Reputation: 30563
In Javascript NaN(Not a Number) is returned whenever you try to do mathematical operation on data which is not a number. You will need to check at which step it is returning string instead of number
Upvotes: 0