Click to See Complete Forum and Search --> : Code error help!


hitman
12-22-2002, 10:06 PM
Hi, I don't really know how to use Javascript. Can you tell me how to align the three tables?
When I type a question (eg: 10 lb to kg) it gives me multiple answers. Please help!

<html>
<head>
<style type="text/css">
// defines the layout classes for the page
body {
background-color:#DFDFDF;
font-family : arial,verdana,helvetica;
font-size:9pt;
}
.title{
font-family : arial,verdana,helvetica;
font-size:10pt;
font-weight:bold;
}

h3 {
font-family : arial,verdana,helvetica;
font-size:9pt;
}
li {
font-family : arial,verdana,helvetica;
font-size:9pt;
}
td,p {
font-family : arial,verdana,helvetica;
font-size:9pt;
}
hr {

border: 1px groove;
}
.main {
background-color:#EFEFEF;
border: 2px groove;
}
.main3 { width:150;
background-color:#C0CDE2;
border: 2px groove;
}
.button {
background-color:#F7D9AC;

}
</style>
<SCRIPT LANGUAGE=JavaScript>
function convert(req_string){
var req_string = document.myform.req_string.value.toLowerCase()
var con = 0
var inch = -5
var cm = -5
var mile = -5
var km = -5
var pounds = -5
var kg = -5
var found_number = -5
var checking = 0
var positive_num = 0
var pattern = " ";
newsite = '<html><head></head><body bgcolor=#DFDFDF><table align=center cellspacing=1 bgcolor="#5F5F5F"><tr><td bgcolor="#EFEFEF"><p style=padding:5><font style="font-family:arial,verdana,helvetica;font-size:9pt;">';
newsite_end = '</font></td></tr></table></body></html>';
words = req_string.split(pattern);
for (i=0; i < words.length; i++) {
if (isNaN(words[i])){
}
else{
con = words[i]
checking = 0
positive_num = i}

if ((words[i])== "inch" || (words[i])== "inches" || (words[i])== "ins"){
cm = i}

if ((words[i])== "centimeter" || (words[i])== "centimeters" || (words[i])== "cm"){
inch = i}
if ((words[i])== "mile" || (words[i])== "miles"){
mile = i}

if ((words[i])== "kilometer" || (words[i])== "kilometers" || (words[i])== "km"){
km = i}

if ((words[i])== "pound" || (words[i])== "pounds" || (words[i])== "lb" || (words[i])== "lbs"){
pounds = i}

if ((words[i])== "kilogram" || (words[i])== "kilograms" || (words[i])== "kg"){
kg = i}
}


if((con == 0)){checking = 2}

if((kg == -5) && (pounds != -5)){checking = 1}

if((pounds == -5) && (kg != -5)){checking = 1}

if((km == -5) && (mile != -5)){checking = 1}

if((mile == -5) && (km != -5)){checking = 1}

if((cm == -5) && (inch != -5)){checking = 1}

if((inch == -5) && (cm != -5)){checking = 1}

if((inch == -5) && (cm == -5) && (km == -5) && (mile == -5) && (kg == -5) && (pounds == -5)){checking = 3}

if((inch == -5) && (cm == -5) && (km == -5) && (mile == -5) && (kg == -5) && (pounds == -5) && (con == 0)){checking = 4}


if((inch != -5) && (cm != -5) || (mile != -5) && (km != -5) || (kg != -5) && (pounds != -5)){
if((positive_num)+ 1 == cm){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con/2.56)*1000)/1000 + " Inches")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(inch < cm){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con * 2.56)*1000)/1000 + " Centimeters")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}

}
if((positive_num) + 1 == inch){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con * 2.56)*1000)/1000 + " Centimeters")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(cm < inch){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con/2.56)*1000)/1000 + " Inches")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
}
if((positive_num)+ 1 == km){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con/1.609)*1000)/1000 + " miles")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(mile < km){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con * 1.609)*1000)/1000 + " kilometers")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
}
if((positive_num) + 1 == mile){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con * 1.609)*1000)/1000 + " kilometers")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(km < mile){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con/1.609)*1000)/1000 + " miles")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
}

if((positive_num) + 1 == kg){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con / 0.45)*1000)/1000 + " Pounds")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(pounds < kg){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con / 0.45)*1000)/1000 + " Pounds")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
}
if(positive_num + 1 == pounds){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con / 2.204623)*1000)/1000 + " Kilograms")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
else{
if(kg < pounds){
document.write(newsite +"You asked: <b>" + req_string + "</b><BR><BR>")
document.write("The answer is " + Math.round((con / 2.204623)*1000)/1000 + " Kilograms")
document.write("<BR><BR>" + "<input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)
checking = 0}
}

}

if((checking == 1)){
document.write(newsite +"You tried to convert two unspeified units please do not")
document.write("<BR>" + "<BR><input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)}
if(checking == 2){
document.write(newsite +"No number was found please leave a space after the number.")
document.write("<BR>" + "<BR><input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)}
if(checking == 3){
document.write(newsite +"Only a number was found please enter measurements.")
document.write("<BR>" + "<BR><input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)}
if(checking == 4){
document.write(newsite +"You did not enter anything in the conversion box.")
document.write("<BR>" + "<BR><input type=button class=button onclick=history.back() value='Next Convert!'>"+newsite_end)}
}
</SCRIPT>
</head>
<table border="0" width="500" align=center class=main>
<tr>
<td>
<BLOCKQUOTE><font class=title>Unit Conversion</font>
<p align=justify> U.C.</p><hr>
<table border="0" width="33%">
<tr><td valign=top>
<table border="9" class=main3>
<tr><th>
Lengths
</th></tr><tr><td><LI>Centimeter</LI>
<LI>Centimeters</LI>
<LI>Cm</LI><BR><BR>
<hr><LI>Inch</LI>
<LI>Inches</LI>
<LI>Ins</LI><hr>
</td></tr></table></td>
<td valign=top>
<table border="9" align=center class=main3><tr><th>
Distances</th></tr><tr><td>
<LI>Kilometer</LI>
<LI>Kilometers</LI>
<LI>Km</LI><br><br><hr>
<LI>Mile</LI>
<LI>Miles</LI><br><br><br><hr>
</td></tr></table></td></tr></table><br><br> <td valign=top>
<table border="9" align=right class=main3>
<tr><th>Weights</th></tr><tr><td>
<LI>Pound</LI>
<LI>Pounds</LI>
<LI>Lb</LI>
<LI>Lbs</LI><br><hr>
<LI>Kilogram</LI>
<LI>Kilograms</LI>
<LI>Kg</LI><br><br><hr> </td></tr></table></td></tr></table><br><br>
<font color="#FF5F00"> Type your request here :</font><BR><BR>
<form name = myform>
<input type = text name = req_string size = 50>
<input type = button value = "Convert" onclick = convert() class=button><br>
</form>
</BLOCKQUOTE></td></tr></table>
</body>
</html>

hitman
12-22-2002, 10:29 PM
Can you just have a look at the code? it seems that it gives multiple answers instead of one.

jeffmott
12-22-2002, 10:49 PM
It would help if you provided comments so we don't have to try and guess at what you were think at each step.

Otherwise, putting in 10 lb to kg seems to convert 10 lb to kb and 10 kg to lb. You'll have to make sure the unit the left of "to" is marked as the starting unit, and the desired unit to the right of "to".

hitman
12-23-2002, 08:16 AM
I did a page that convert units. It uses free text, so you can type a sentence that includes a number, the first unit and the second unit that has to be converted into.
The problem is that when you type for exemple ' convert 12 inches into cm' it give an wrong answer.

Can you have a look at the code please?