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


Groups > comp.lang.javascript > #17180

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

Newsgroups comp.lang.javascript
Date 2012-11-11 12:45 -0800
References (1 earlier) <aga4cnF1f0vU1@mid.individual.net> <f361ca82-911a-4f52-9132-3f0af5baf3ca@googlegroups.com> <aga76fF248tU1@mid.individual.net> <f4fcb90e-7930-4622-a518-472813d85cab@googlegroups.com> <XnsA10921EAC6664jaejunksglegooilma@0.0.0.9>
Message-ID <465c0f13-6b2e-4802-9262-cf38bb8a1195@googlegroups.com> (permalink)
Subject Re: JavaScript Case Project 3-1: The dreaded NaN error
From dmn2004404@gmail.com

Show all headers | View raw


On Sunday, November 11, 2012 2:21:51 PM UTC-6, JJ wrote:
> dmn2004404@gmail.com wrote:
> 
> > Wait -- WAIT! I think I got it -- almost. I've had to make a few
> 
> > changes, as follows. Here's my revised JavaScript: 
> 
> > 
> 
> > <script type="text/javascript">
> 
> > var shipping, total;
> 
> > function calculateShipping(purchasePrice) {
> 
> >     if (purchasePrice <= 25)
> 
> >         shipping = 1.50;
> 
> >     else
> 
> >         shipping = purchasePrice * .10;
> 
> >     total = purchasePrice + shipping;
> 
> >      window.alert('Purchase price plus shipping and handling is $' +
> 
> >      total); 
> 
> > }
> 
> > </script>
> 
> > 
> 
> > and the revised input tag for the button:
> 
> > 
> 
> > <input type="submit" name="btnCalculate" id="btnCalculate"
> 
> > value="Calculate"
> 
> > onclick="calculateShipping(parseFloat(txtPurchasePrice.value,10))" /> 
> 
> > 
> 
> > I believe the key was actually to put the value of the text box INSIDE
> 
> > the calculateShipping() function as a parameter. 
> 
> > 
> 
> > Anyway, with these revisions, I tried the 123.45 value you attempted,
> 
> > and I got the LONG floating value of 135.79500000000002 -- which
> 
> > looked a little messy. So I attached the "toFixed(2)" method to
> 
> > "totalCost" at the end of the "window.alert" command, and it worked. 
> 
> > 
> 
> > But now, I'd like to validate user box input so that if the user
> 
> > should enter a zero, a negative number, or something that actually IS
> 
> > "NaN" (e.g. "Hello world!"), the page will produce a dialog box that
> 
> > says the value is invalid. How might I go about doing that? 
> 
> 
> 
> Make the calculateShipping function to return a boolean value. Returns 
> 
> true to allow submit or false to disallow it. i.e.: due to invalid form 
> 
> input(s).
> 
> 
> 
> Also check for NaN result each and every parseFloat using isNaN.

I think I finally got it! I added an extra if...else statement to make it complete. Here's my final script:

<script type="text/javascript"> 
var shipping, total; 
function calculateShipping(purchasePrice) { 
    if (purchasePrice > 0) {
        if (purchasePrice > 25) 
            shipping = purchasePrice * .10; 
        else 
            shipping = 1.50; 
        total = purchasePrice + shipping; 
        window.alert('Purchase price plus shipping and handling is $' + total);
    } else
        window.alert('Invalid value for purchase price! Please try again.');
}
</script>

Thanks guys, for all your help -- and to think I was on the verge of throwing in the towel on this one!

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