JS newbie question...

D

Dirntknow

I'm creating my first CSS driven website for a local running club and would
like to incorporate a page where a visitor can input various information
such as distance in miles and convert to kilometers and perhaps calculate
average speeds etc. I've found something similar and adapted it but the
average speed is displaying far too many decimal places! I'm a complete
newbie to JS so any help in achieving my aim would be much appreciated!

Thanks...Andre

<form>
<p>Amount in miles :
<input name="miles"type="text" size="5"></p>
<p><input type="button" value="Convert to kilometers";" onclick="kilometers
= Math.round( form.miles.value * 1.61 * 100) / 100; alert( 'Amount in
Kilometers is ' +kilometers);"></p>
</form>
<form>
<p>Amount in kilometers:
<input name="kilometers"type="text" size="5"></p>
<p><input type="button" value="Convert to miles";" onclick="miles =
Math.round( form.kilometers.value * 0.621 * 100) / 100; alert( 'Amount in
miles is ' +miles);"></p>
</form>
<form>
<p>Distance you ran:
<input name="distance"type="text" size="5"></p>

<p>Your time (in minutes):
<input name="time"type="text" size="5"></p>
<p><input type="button" value="convert to average speed";" onclick="avespeed
= Math.round(form.distance.value * 100) / (form.time.value); alert( 'Average
speed is:'+avespeed+'mph');"></p>
</form>
 
D

Darko

I'm creating my first CSS driven website for a local running club and would
like to incorporate a page where a visitor can input various information
such as distance in miles and convert to kilometers and perhaps calculate
average speeds etc. I've found something similar and adapted it but the
average speed is displaying far too many decimal places! I'm a complete
newbie to JS so any help in achieving my aim would be much appreciated!

Thanks...Andre

<form>
<p>Amount in miles :
<input name="miles"type="text" size="5"></p>
<p><input type="button" value="Convert to kilometers";" onclick="kilometers
= Math.round( form.miles.value * 1.61 * 100) / 100; alert( 'Amount in
Kilometers is ' +kilometers);"></p>
</form>
<form>
<p>Amount in kilometers:
<input name="kilometers"type="text" size="5"></p>
<p><input type="button" value="Convert to miles";" onclick="miles =
Math.round( form.kilometers.value * 0.621 * 100) / 100; alert( 'Amount in
miles is ' +miles);"></p>
</form>
<form>
<p>Distance you ran:
<input name="distance"type="text" size="5"></p>

<p>Your time (in minutes):
<input name="time"type="text" size="5"></p>
<p><input type="button" value="convert to average speed";" onclick="avespeed
= Math.round(form.distance.value * 100) / (form.time.value); alert( 'Average
speed is:'+avespeed+'mph');"></p>
</form>

use .toFixed method to achieve a wanted number of decimal spaces.
Google for it, it's quite simple.
 
R

RobG

I'm creating my first CSS driven website for a local running club and would
like to incorporate a page where a visitor can input various information
such as distance in miles and convert to kilometers and perhaps calculate
average speeds etc. I've found something similar and adapted it but the
average speed is displaying far too many decimal places! I'm a complete
newbie to JS so any help in achieving my aim would be much appreciated!

Thanks...Andre

<form>
<p>Amount in miles :
<input name="miles"type="text" size="5"></p>
<p><input type="button" value="Convert to kilometers";" onclick="kilometers
= Math.round( form.miles.value * 1.61 * 100) / 100; alert( 'Amount in

In most place, 1,000 metres is 1 kilometre. To save argument, perhaps
km is better. :)

There appears to be some wayward characters: ;"

It doesn't seem sensible to multiply by 100 then immediately divide by
100. Do all the mathematics first, then round:

= Math.round( this.form.miles.value * 1.61); alert(...

You will find lots of information about rounding here:

<URL: http://www.merlyn.demon.co.uk/js-round.htm >
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Thu, 1 Mar 2007 03:41:07, Darko

That should be 1.609, to be reasonably accurate.

Will you do a European version, converting to kilometres ?

If you have previously multiplied by about 1.6, you should here divide
by the identical number.

The correct multiplier is 63360 * 254 / 1e7 which is exactly 1.609344
and calculates as such by javascript. FYI, 63360 * 254 * 1e-7
calculates as a mere 1.6093439999999998 or thereabouts.

You might as well use the correct figure, since it is trivial to do so.

63360 is 8 * 10 * 22 * 3 * 12.


use .toFixed method to achieve a wanted number of decimal spaces.
Google for it, it's quite simple.

But .toFixed is not available in all browsers, and is also not reliable.

In principle, the OP could either round to a fixed number of decimal
places, or to a fixed number of significant figures. See my cited site.
But I suppose that rummers rarely run less than 80 yards or more than
about 26 miles, so the distinction may be unimportant unless you want to
do seasonal or other cumulative distances.


It's a good idea to read the newsgroup and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top