Connecting Tech Pros Worldwide Forums | Help | Site Map

Time/distance converter help (ptII)

Dirntknow
Guest
 
Posts: n/a
#1: Mar 3 '07
Further to my recent post in this newsgroup i've got to admit i'm
struggling. I've no experience with javascript or it's workings. What i'd
like is a simple converter to allow a user to input the distance travelled
in miles and the time in hours and minutes, the result being an average
speed. What i'd appreciate is for someone to write the script for me and
post here if possible. I am willing to learn but don't know where to start
and i've found no tutorial that will help me!

Thanks...Andre



OmegaJunior
Guest
 
Posts: n/a
#2: Mar 3 '07

re: Time/distance converter help (ptII)


On Sat, 03 Mar 2007 22:10:04 +0100, Dirntknow <nospam@please.comwrote:
Quote:
Further to my recent post in this newsgroup i've got to admit i'm
struggling. I've no experience with javascript or it's workings. What i'd
like is a simple converter to allow a user to input the distance
travelled
in miles and the time in hours and minutes, the result being an average
speed. What i'd appreciate is for someone to write the script for me and
post here if possible. I am willing to learn but don't know where to
start
and i've found no tutorial that will help me!
>
Thanks...Andre
>
>
Assuming you want to run this script in a web page:

<html>
<head>
<title>Converting distance and time into speed</title>
<script type="text/javascript">
function Calculate() {
var theDistance = new Number(document.forms['theForm'].txtDistance.value);
var theTimeUsed = new Number(document.forms['theForm'].txtTimeUsed.value);
if(!NaN(theDistance)&&!NaN(theTimeUsed)&&(theTimeU sed>0)) {
document.forms['theForm'].txtResult = (theDistance / (theTimeUsed/60));
return true;
}
return false;
}
</script>
</head>
<body>
<h1>Converting distance and time into speed</h1>
<form name="theForm" id="theForm" action="#" onsubmit="Calculate();return
false;">
<fieldset><legend>Enter information:</legend>
<p><label for="txtDistance">Distance: <input type="text"
name="txtDistance" id="txtDistance" value="0" /(in miles).</label></p>
<p><label for="txtTimeUsed">Time Used: <input type="text"
name="txtTimeUsed" id="txtTimeUsed" value="0" /(in minutes).</label></p>
</fieldset>
<fieldset><legend>Options</legend>
<p><input type="reset" value="Cancel" /<input type="submit"
value="Calculate!" /></p>
</fieldset>
<fieldset><legend>Result</legend>
<p><label for="txtResult">Average speed: <input type="text"
name="txtResult" id="txtResult" value="0" /(miles per hour).</p>
</fieldset>
</body>
</html>



--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
OmegaJunior
Guest
 
Posts: n/a
#3: Mar 3 '07

re: Time/distance converter help (ptII)


On Sat, 03 Mar 2007 22:26:34 +0100, OmegaJunior
<omegajunior@spamremove.omegajunior.netwrote:
Quote:
On Sat, 03 Mar 2007 22:10:04 +0100, Dirntknow <nospam@please.comwrote:
>
Quote:
>Further to my recent post in this newsgroup i've got to admit i'm
>struggling. I've no experience with javascript or it's workings. What
>i'd
>like is a simple converter to allow a user to input the distance
>travelled
>in miles and the time in hours and minutes, the result being an average
>speed. What i'd appreciate is for someone to write the script for me and
>post here if possible. I am willing to learn but don't know where to
>start
>and i've found no tutorial that will help me!
>>
>Thanks...Andre
>>
>>
>
Assuming you want to run this script in a web page:
>
<html>
<head>
<title>Converting distance and time into speed</title>
<script type="text/javascript">
function Calculate() {
var theDistance = new
Number(document.forms['theForm'].txtDistance.value);
var theTimeUsed = new
Number(document.forms['theForm'].txtTimeUsed.value);
if(!NaN(theDistance)&&!NaN(theTimeUsed)&&(theTimeU sed>0)) {
document.forms['theForm'].txtResult = (theDistance /
(theTimeUsed/60));
return true;
}
return false;
}
</script>
</head>
<body>
<h1>Converting distance and time into speed</h1>
<form name="theForm" id="theForm" action="#"
onsubmit="Calculate();return false;">
<fieldset><legend>Enter information:</legend>
<p><label for="txtDistance">Distance: <input type="text"
name="txtDistance" id="txtDistance" value="0" /(in miles).</label></p>
<p><label for="txtTimeUsed">Time Used: <input type="text"
name="txtTimeUsed" id="txtTimeUsed" value="0" /(in
minutes).</label></p>
</fieldset>
<fieldset><legend>Options</legend>
<p><input type="reset" value="Cancel" /<input type="submit"
value="Calculate!" /></p>
</fieldset>
<fieldset><legend>Result</legend>
<p><label for="txtResult">Average speed: <input type="text"
name="txtResult" id="txtResult" value="0" /(miles per hour).</p>
</fieldset>
</body>
</html>
>
>
>
Ack. Forgot </formbefore </body>.

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Richard Cornford
Guest
 
Posts: n/a
#4: Mar 3 '07

re: Time/distance converter help (ptII)


OmegaJunior <omegajunior@spamremove.omegajunior.netwrote:
<snip>
Quote:
if(!NaN(theDistance)&&!NaN(theTimeUsed)&&(theTimeU sed>0)) {
<snip ^^^^^^^^^^^^^^^^

Attempting to call a number is not going to be effective.

Richard.
OmegaJunior
Guest
 
Posts: n/a
#5: Mar 3 '07

re: Time/distance converter help (ptII)


On Sat, 03 Mar 2007 23:46:57 +0100, Richard Cornford
<Richard@litotes.demon.co.ukwrote:
Quote:
OmegaJunior <omegajunior@spamremove.omegajunior.netwrote:
<snip>
Quote:
> if(!NaN(theDistance)&&!NaN(theTimeUsed)&&(theTimeU sed>0)) {
<snip ^^^^^^^^^^^^^^^^
>
Attempting to call a number is not going to be effective.
>
Richard.
Erm, yeah. NaN() should be isNaN(). (Hope you meant to point that out.)


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Dirntknow
Guest
 
Posts: n/a
#6: Mar 4 '07

re: Time/distance converter help (ptII)


Sorry, doesn't work for me!! :-(


Michael White
Guest
 
Posts: n/a
#7: Mar 4 '07

re: Time/distance converter help (ptII)


Dirntknow wrote:
Quote:
Further to my recent post in this newsgroup i've got to admit i'm
struggling. I've no experience with javascript or it's workings. What i'd
like is a simple converter to allow a user to input the distance travelled
in miles and the time in hours and minutes, the result being an average
speed. What i'd appreciate is for someone to write the script for me and
post here if possible. I am willing to learn but don't know where to start
and i've found no tutorial that will help me!
>
function getSpeed(distance,hours,minutes,precision){// distance: Miles
return toFixed(distance/(hours + minutes/60),precision?precision:2) +"MPH";
}
However make sure user's input is valid, see discussion in an earlier
post by John Stockton.
Mick

Mick
RobG
Guest
 
Posts: n/a
#8: Mar 4 '07

re: Time/distance converter help (ptII)


On Mar 4, 7:10 am, "Dirntknow" <nos...@please.comwrote:
Quote:
Further to my recent post in this newsgroup i've got to admit i'm
struggling. I've no experience with javascript or it's workings.
You probably wont find a single tutorial to teach you everything
that's needed, you'll need to learn about javascript in general then
apply the knowledge to problem.

You need to learn how to get values from form controls, validate the
input, display error messages, do calculations and format and present
the results.

The following should get you started, I've used an alert for the error
messages but it is much better to write them to the page in an
appropriate spot. I'll leave that to you:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<title>Units per Hour</title>
<style type="text/css">
.label {text-align: right;}
</style>

<script type="text/javascript">

// The following set of functions adds a toFixed method
// to the built-in Number object
// From clj FAQ: http://www.jibbering.com/faq/#FAQ4_6
function Stretch(Q, L, c) { var S = Q
if (c.length>0) while (S.length<L) { S = c+S }
return S
}
function StrU(X, M, N) { // X>=0.0
var T, S=new String(Math.round(X*Number("1e"+N)))
if (S.search && S.search(/\D/)!=-1) { return ''+X }
with (new String(Stretch(S, M+N, '0')))
return substring(0, T=(length-N)) + '.' + substring(T)
}
function Sign(X) { return X<0 ? '-' : ''; }
function StrS(X, M, N) { return Sign(X)+StrU(Math.abs(X), M, N) }
Number.prototype.toFixed= function(n){ return StrS(this,1,n)};

// Check a possible float - decimal place is optional
function validNum(n) {
return /^\d+\.?\d*$/.test(n);
}

// Check an int
function validInt(n) {
return /^\d+$/.test(n);
}

// Convert units to per hour
function unitsPerHr(form) {
var rate;
var dist = form.distance.value;
var time = form.time.value.split(':');
var hr = time[0];
var min = time[1];
var sec = time[2];
var err = [];

// Check distance is a valid number
if (!validNum(dist)) {
err.push('Quantity must be a valid number, e.g. 23.5');
}

// Check time parts are valid
if (!validInt(hr) || !validInt(min) || !validNum(sec)) {
err.push('Time must be hour:min:sec, e.g. 2:23:15.5');
}

// Check time numbers are within range
if (min 59 || sec >= 60) {
err.push('Minutes and seconds must be less than 60');
}

// Convert time to decimal hours:
hr = +hr + min/60 + sec/3600;

if (hr 0) {
rate = (dist/hr).toFixed(2);
} else {
err.push('Time must be greater than zero')
}

if (err.length 0) {
alert(err.join('\n\n'));
} else {
document.getElementById('result').innerHTML =
rate + ' units per hour';
}
return false;
}

</script>

<form action="" onsubmit="return unitsPerHr(this);">
<table>
<tr>
<td class="label">Enter quantity:
<td><input type="text" name="distance" value="0">
<tr>
<td class="label">Enter time<br>hr:min:sec:
<td><input type="text" name="time" value="0:0:0">
<tr>
<td class="label"><input type="submit" value="Calculate">
<td><span id="result"></span>
</table>
</form>


--
Rob

Michael White
Guest
 
Posts: n/a
#9: Mar 4 '07

re: Time/distance converter help (ptII)


Michael White wrote:
Quote:
Dirntknow wrote:
>
Quote:
>Further to my recent post in this newsgroup i've got to admit i'm
>struggling. I've no experience with javascript or it's workings. What
>i'd like is a simple converter to allow a user to input the distance
>travelled in miles and the time in hours and minutes, the result being
>an average speed. What i'd appreciate is for someone to write the
>script for me and post here if possible. I am willing to learn but
>don't know where to start and i've found no tutorial that will help me!
>>
function getSpeed(distance,hours,minutes,precision){// distance: Miles
return toFixed(distance/(hours + minutes/60),precision?precision:2) +"MPH";
}

That should be:
return (distance/(hours + minutes/60)).toFixed(precision?precision:2)
+"MPH";
M
Quote:
However make sure user's input is valid, see discussion in an earlier
post by John Stockton.
Mick
>
Mick
VK
Guest
 
Posts: n/a
#10: Mar 4 '07

re: Time/distance converter help (ptII)


toFixed(precision?precision:2)

That would prevent 0 signs in fraction, enforcing makeToFixed(0)
transformed to makeToFixed(2)
Unless it's a contextually useful default, one should prevent 0/false
ambiguousity by something like toFixed((typeof precision == 'number')?
precision:2)

Michael White
Guest
 
Posts: n/a
#11: Mar 4 '07

re: Time/distance converter help (ptII)


VK wrote:
Quote:
Quote:
>>toFixed(precision?precision:2)
>
>
That would prevent 0 signs in fraction, enforcing makeToFixed(0)
transformed to makeToFixed(2)
Unless it's a contextually useful default, one should prevent 0/false
ambiguousity by something like toFixed((typeof precision == 'number')?
precision:2)
>
Good point.
Mick
Dr J R Stockton
Guest
 
Posts: n/a
#12: Mar 4 '07

re: Time/distance converter help (ptII)


In comp.lang.javascript message <1173005001.691919.199740@t69g2000cwt.go
oglegroups.com>, Sun, 4 Mar 2007 02:43:21, RobG <rgqld@iinet.net.au>
posted:
Quote:
function StrU(X, M, N) { // X>=0.0
var T, S=new String(Math.round(X*Number("1e"+N)))
if (S.search && S.search(/\D/)!=-1) { return ''+X }
with (new String(Stretch(S, M+N, '0')))
return substring(0, T=(length-N)) + '.' + substring(T)
}
FYI, that's not what its author currently uses and recommends.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Closed Thread