Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17168

Re: JavaScript Case Project 3-1: The dreaded NaN error

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From "Mel Smith" <med_cutout_syntel@aol.com>
Newsgroups comp.lang.javascript
Subject Re: JavaScript Case Project 3-1: The dreaded NaN error
Date Sun, 11 Nov 2012 10:08:26 -0700
Lines 59
Message-ID <aga4cnF1f0vU1@mid.individual.net> (permalink)
References <055e7215-93ba-40a9-81f3-312ea0e4f5e2@googlegroups.com>
X-Trace individual.net FQ9ro3zRN65O552iBK/3ygHdyqEqBwJisXoE1Z5REHAvTxCleH8p/OpusEVWCqrYng
Cancel-Lock sha1:DBgs4kkO0JEolYWP1wHXKN7yYrc=
X-Priority 3
X-MSMail-Priority Normal
X-Newsreader Microsoft Outlook Express 6.00.2900.5931
X-RFC2646 Format=Flowed; Original
X-MimeOLE Produced By Microsoft MimeOLE V6.00.2900.6157
Xref csiph.com comp.lang.javascript:17168

Show key headers only | View raw


where I'm going wrong?

    You must convert the text string input to a floating point number --  
probably using the parseFloat() function. See below

-Mel Smith (one neophyte to another)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
var purchasePrice = txtPurchasePrice.value, shippingAndHandling, totalCost;
function calculateShipping() {

    // in here you need to convert the txtPurchasePrice to a floating point 
numeric before
    // you can start your calculations. Perhaps like the following:

    purchasePrice = 
parseFloat(document.getElementById("txtPurchasePrice").value) ;

    if (purchasePrice <= 25)
        shippingAndHandling = 1.50;
    else
        shippingAndHandling = purchasePrice * .10;
    totalCost = purchasePrice + shippingAndHandling;

    // now you should format your totalCost to an appropriate number of 
digits below

window.alert('Purchase price plus shipping and handling is $' + totalCost);
}
</script>
</head>

<body>
Purchase price (no dollar signs):
<form id="form1" method="post" action="">
  <p>
    <label for="txtPurchasePrice"></label>
    <input type="text" name="txtPurchasePrice" id="txtPurchasePrice" />
    <input type="submit" name="btnCalculate" id="btnCalculate" 
value="Calculate" onclick="calculateShipping()" />
  </p>
</form>
</body>
</html>

For the record, I've also tried changing "txtPurchasePrice.value" to 
"document.getElementById('txtPurchasePrice').value" and even 
"txtPurchasePrice.text", but none of what I'm doing appears to make any 
difference -- no matter which way I go, it still says NaN. 

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 08:28 -0800
  Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:08 -0700
    Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 09:34 -0800
      Re: JavaScript Case Project 3-1: The dreaded NaN error "Mel Smith" <med_cutout_syntel@aol.com> - 2012-11-11 10:56 -0700
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:07 -0800
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 10:52 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:15 +0000
            Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:45 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error JJ <jaejunks@glegooilma-swapit.com> - 2012-11-11 20:11 +0000
        Re: JavaScript Case Project 3-1: The dreaded NaN error dmn2004404@gmail.com - 2012-11-11 12:15 -0800
          Re: JavaScript Case Project 3-1: The dreaded NaN error Osmo Saarikumpu <osmo@weppipakki.com> - 2012-11-12 10:10 +0200
  Re: JavaScript Case Project 3-1: The dreaded NaN error Gregor Kofler <usenet@gregorkofler.com> - 2012-11-12 09:51 +0100
    Re: JavaScript Case Project 3-1: The dreaded NaN error Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-11-12 11:49 +0100

csiph-web