Topic: Converter from String Pages that link to <a href="https://ozoneasylum.com/backlink?for=28076" title="Pages that link to Topic: Converter from String" rel="nofollow" >Topic: Converter from String\

 
Author Thread
Hutch
Obsessive-Compulsive (I) Inmate

From:
Insane since: May 2006

IP logged posted posted 06-14-2006 00:21 Edit Quote

I am also trying to do a converter using a string input. e.g.: How many meters in 5 miles?
It will pull the "meters" the "5" and the "miles" from the string, do the math, and return the output.

code:
<html>
<head>
<title>String Converter</title>

<script language="Javascript" type="javascript/text">

function Convert (string1) {

if (/\smeters\s.+\d+.*\smiles/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/0.0006214;
document.form1.output.value=num1 +'miles is equal to' + num2 'meters.';
}


if (/\sinches\s.+\d+.*\smeters/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/0.00254;
document.form1.output.value=num1 +'meters is equal to' + num2 'inches.';
}

if (/\scentimeters\s.+\d+.*\sinches/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/0.3937;
document.form1.output.value=num1 +'inches is equal to' + num2 'centimeters.';
}

if (/\scentimeters\s.+\d+.*\smiles/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/0.00000621371192;
document.form1.output.value=num1 +'miles is equal to' + num2 'centimetres.';
}

if (/\slitres\s.+\d+.*\squarts/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/1.05669;
document.form1.output.value=num1 +'US quarts is equal to' + num2 'litres.';
}

if (/\sgrams\s.+\d+.*\spounds/i.test(string1)){
var num1=Number(/\d+/.exec(string1));
var num2=num1/0.002205;
document.form1.output.value=num1 +'miles is equal to' + num2 'meters.';
}
else{
alert('Please enter a valid conversion');
document.form1.input.focus();
}
}


</script>

</head>

<body>
This conversion tool accepts the following conversions:<br>
meters -> miles<br>
inches -> meters<br>
centimeters -> inches<br>
centimeters -> miles<br>
litres -> quarts<br>
grams -> pounds<br>

in the form:<br>
e.g. "How many inches in 5 meters?"

<form name="form1" onsubmit="return false;">
Please enter the string you wish to convert:
<input type="text" name="original">
<p>
<input type="button" value="Convert!" onclick="Convert(document.form1.original.value);">
<p>
<textarea name="output" cols=40 rows=25></textarea>
</form>
</body>
</html>



(Edited by Hutch on 06-14-2006 00:22)



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu