开发者

How to change the content of a variable to zero if isNaN [duplicate]

开发者 https://www.devze.com 2023-04-12 11:08 出处:网络
This question already has answers here:开发者_JS百科 Closed 11 years ago. Possible Duplicate: When I press enter I get isNaN, but the value is a number
This question already has answers here:开发者_JS百科 Closed 11 years ago.

Possible Duplicate:

When I press enter I get isNaN, but the value is a number

I turn in my homework last Sunday.It was sent back to me for correction because the isNaN value was returned to the Total textbox.I thought that is what is was programmed to do. Instead, according to her, "The isNaN function will examine the variable and determine if the variable holds non-numeric data, then the next statement will change the content of the variable to set to zero," which has me confused. Any suggestions Here is the code. *The "return;" statements were not a syntax error before I made changes. Here is the link to the uploaded file: ciswebs.smc.edu/cis54/tyson_schweidel/homework2.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Price Calculator</title>
<script type="text/javascript">

function fixOrder()
{
var numPrice = parseFloat(document.getElementById("cost").value);
var taxP = parseFloat(document.getElementById("tax").value);
var total = parseFloat(document.getElementById("total").value);

}

if (isNaN(numPrice)){

    alert("Sorry,you must enter a numeric value to place order");

if (isNaN(taxP))
    alert("Sorry, you must enter a numeric tax value to continue");
    return; //gets syntax error
}


var tax = (numPrice * taxP)/100;{
var total = numPrice + tax;
numPrice = 0;
taxP = 0;
    document.getElementById("total").value = "$" + total.toFixed(2);
return; //gets syntax error
}
</script>

</head>

<body bgcolor="#00f3F1">


<h1 align="left">Price Calculator</h1>

<form name="form">
<p>
Price:  <input type="text" id="cost" name="cost" value="" onchange="fixOrder();"/>
</p>
<p>
Tax: &nbsp;
<input type="text" id="tax" name="tax" value="" onchange="fixOrder();"/>
</p>
<p>
Total: 
<input type="text" id="total" name="total" value="" disabled="disabled();"/>
</p>
</form>


your formatting is messed up and caused you to exit the function fixOrder before it really did anything:

function fixOrder() {
    var numPrice = parseFloat(document.getElementById("cost").value);
    var taxP = parseFloat(document.getElementById("tax").value);
    var total = parseFloat(document.getElementById("total").value);



    if (isNaN(numPrice)) {
        alert("Sorry,you must enter a numeric value to place order");
        return; //gets syntax error
    }

    if (isNaN(taxP)) {
        alert("Sorry, you must enter a numeric tax value to continue");
        return;
    }

    var tax = (numPrice * taxP) / 100;
    var total = numPrice + tax;
    numPrice = 0;
    taxP = 0;
    document.getElementById("total").value = "$" + total.toFixed(2);
    return; //gets syntax error
}

See how much easier to read that is? Where you original code could be reduced to :

document.getElementById("total").value = "$NaN";


You variables are not in scope when the isNaN() function attempts to check them.

0

精彩评论

暂无评论...
验证码 换一张
取 消