John Smith
John Smith

Reputation: 9

What is causing this to return NaN?

This is supposed to be a simple order calculator, but for some reason it's just not working. Here's the code:

var name, product, price, discount, quantity;

var name = prompt("What is your name?", "Enter name");
var sentence = "Hello " + name + " please look through our available products and services before placing your order.";
alert(sentence);

var product = prompt("Please enter the name of the product you are looking to purchase from the table.", "Enter product");
var quantity = 1*prompt("How many " + product + " would you like to purchase?", "Enter quantity");

var cost = price * quantity;
var orderdiscount = price * discount * quantity;
var totalcost = cost - orderdiscount;

a = confirm(+ name + ", you ordered " + quantity + " of " + product + ". Is this correct?");

if (a) {
    total = cost - (price * discount * quantity);
        if (product === "ice cream cake") {
                price = 20;
                discount = .15;
        } else if (product === "ice cream cone") {
                price = 3;
                discount = .01;
        } else if (product === "small ice cream sundae") {
                price = 5;
                discount = .05;
        } else if (product === "large ice cream sundae") {
                price = 6;
                discount = .05;
        } else if (prompt = ("Sorry, " + name + ". You entered an invalid product. Refresh the page to reload and place the order again.")) {
        }
        }

else 
{
    alert("Refresh the page to reload and place a new order");
}

document.write("Thank you for placing an order with us, " + name + ".");
document.write("</br>");
document.write("The cost of buying " + quantity + " of " + product + " is " + cost + ".");
document.write("</br>");
document.write("The discount for this purchase is " + orderdiscount + ".");
document.write("</br>");
document.write("With the discount, your total order cost is " + totalcost + ".");

When I load the page and enter all of the necessary information, it returns:

"Thank you for placing an order with us, . The cost of buying 1 of ice cream cake is NaN. The discount for this purchase is NaN. With the discount, your total order cost is NaN."

There are supposed to be calculated numbers in place of those NaNs. What in this code is causing this to happen?

Upvotes: 0

Views: 653

Answers (7)

fjxx
fjxx

Reputation: 945

Use parseInt(..) on return value of :

prompt("How many " + product + " would you like to purchase?", "Enter quantity")

Upvotes: 1

Maverick Jones
Maverick Jones

Reputation: 434

The main problem I can see it the cost variable using the price variable before it has been assigned a value, first add simply change var cost = price * quantity; to this var cost = function(){return price*quantity;}; this way each time you call cost it will recalculate the price*quantity rather than at the top of the file before you've done the operations to get the price. This happens because until here if (product === "ice cream cake") { price = 20; discount = .15; } else if (product === "ice cream cone") { price = 3; discount = .01; price and discount are Null.

Upvotes: 1

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

price is undefined for a kickoff, but even so, if the user was to order a dozen or twelve instead of 12 of whatever item he/she orders, you'll still end up with a NaN when quantity = 1*'a dozen';. Checking user input will always be a necessity

Upvotes: 0

Jonah
Jonah

Reputation: 283

Is it the fact you're not declaring the price? If it's not a needed field for your user. Hide it.

Jonah

Upvotes: -1

undefined
undefined

Reputation: 2101

You're calculating cost before You set the price.

Upvotes: 0

musefan
musefan

Reputation: 48415

I would say it is because the value of price is not set anywhere and is left undefined by the time it is used.

Therefore when you calculate cost you are multiplying price (which is undefined) by quantity, and thus you will not get a valid result

Upvotes: 2

epascarello
epascarello

Reputation: 207501

You are doing your calculation before you are setting the values of your variables.

    var cost = price * quantity;  <-- calculation here
    ....

    total = cost - (price * discount * quantity);  <-- calculation here
    if (product === "ice cream cake") {
            price = 20;
            discount = .15;  <-- setting variable here
    }

Upvotes: 7

Related Questions