• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Metric Convertor......Pls check the code

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<html>
<head>
<title>METERIC CONVERTOR</title>
<script language=javascript>
function conversion(thisForm) {
switch(thisForm.from.value) {

case 1: thisForm.Result.value = ( ((thisForm.Amount.value-32)*5)/9 );
break;
case 2: thisForm.Result.value = ( ((thisForm.Amount.value * 9)/5)+32 );
break;
case 3: thisForm.Result.value = ( thisForm.Amount.value * 1.61 );
break;
case 4: thisForm.Result.value = ( thisForm.Amount.value * 0.62 );
break;
case 5: thisForm.Result.value = ( thisForm.Amount.value * 0.92 );
break;
case 6: thisForm.Result.value = ( thisForm.Amount.value * 1.09 );
break;
case 7: thisForm.Result.value = ( thisForm.Amount.value * 0.3 );
break;
case 8: thisForm.Result.value = ( thisForm.Amount.value * 3.28 );
break;
case 9: thisForm.Result.value = ( thisForm.Amount.value * 2.54 );
break;
case 10: thisForm.Result.value = ( thisForm.Amount.value * 0.39 );
break;
case 11: thisForm.Result.value = ( thisForm.Amount.value * 0.45 );
break;
case 12: thisForm.Result.value = ( thisForm.Amount.value * 2.22 );
break;
case 13: thisForm.Result.value = ( thisForm.Amount.value * 28.35 );
break;
case 14: thisForm.Result.value = ( thisForm.Amount.value * 0.035 );
break;
case 15: thisForm.Result.value = ( thisForm.Amount.value * 3.79 );
break;
case 16: thisForm.Result.value = ( thisForm.Amount.value * 0.26 );
break;
case 17: thisForm.Result.value = ( thisForm.Amount.value * 4.55 );
break;
case 18: thisForm.Result.value = ( thisForm.Amount.value * 0.22 );
break;
}
}
</script>
</head>
<body bgcolor=#009999>
<center>
<font face=verdana size=5 color=#336699><b><l>METERIC CONVERTOR</l></b></font>
<br><br>
<font face=verdana size=2 color=#003366><b>Convert from US to Metric amounts or vice versa.</b></font><br><br>
<font face=verdana size=2 color=#006699><b>Pick the conversion you'd like to make<br>and enter an amount</b></font><br><br><br>
<form>
<table width=60% cellspacing=1 cellpadding=4 align=middle>
<tr>
<td width=40% bgcolor=#eeeeee><font face=verdana size=2><b>Convert:</b></font></td>
<td width=60% bgcolor=#cccccc><select name=from>
<option selected value=1>Celsius -> Fahrenheit</option>
<option value=2>Fahrenheit -> Celcius</option>
<option value=->---</option>
<option value=3>mile -> kilometer</option>
<option value=4>kilometer -> mile</option>
<option value=5>yard -> meter</option>
<option value=6>meter -> yard</option>
<option value=7>foot -> meter</option>
<option value=8>meter -> foot</option>
<option value=9>inch -> centimeter</option>
<option value=10>centimeter -> inch</option>
<option value=->---</option>
<option value=11>pound -> kilogram</option>
<option value=12>kilogram -> pound</option>
<option value=13>ounce -> gram</option>
<option value=14>gram -> ounce</option>
<option value=->---</option>
<option value=15>US gallon -> litre</option>
<option value=16>litre -> US gallon</option>
<option value=17>imp.gallon -> litre</option>
<option value=18>litre -> imp.gallon</option>
</select>
</td>
</tr>
<tr>
<td width=40% bgcolor=#eeeeee><font face=verdana size=2><b>Amount:</b></font></td>
<td width=60% bgcolor=#cccccc><input type=text name=Amount value=""></td>
</tr>
<tr>
<td width=40% bgcolor=#eeeeee><font face=verdana size=2><b>Result:</b></font></td>
<td width=60% bgcolor=#cccccc><input type=text name=Result value=""></td>
</tr>
<tr>
<td width=40% bgcolor=#eeeeee><font face=verdana size=2><b>And Now:</b></font></td>
<td width=60% bgcolor=#cccccc><input type=button value=Calculate onClick="conversion(form)"></td>
</tr>
</table>
</form>
</body>
</html>
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is with the way you are calling the value in your select list. With select lists you have to do a 2 step process to get the value. First you get the selectIndex that was chosen and then use this value to get the value of the item at that selectedIndex. You can combine this in one step with the following code. Paste this into x for the switch(x) part of your code and it should work:
switch(thisForm.from.options[thisForm.from.selectedIndex].value)
Bill
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill,
Thanks for going through my code and seeing what is wrong with it. I am learning JavaScript by my self. I will make the changes as you have suggested and see it it works and will come back to you.
Thanks,
Regards
Raj.
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill,
I have gone through what you have suggested, which made sense theoritically but i am still not able to make the thing work. The thing is that i write my html/JavaScript code in Notepad and run it on Browser. I don't have any tool to debug the thing so that I come to know where I am going wrong. Can you pls try out the code and see where I am wrong. Pls check it out?
Thanks
Regards
Raj.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did some more testing and I see what is going on. The code I gave you works in Netscape but not in IE. This is how I went about finding what was wrong so you can try these steps when you are doing debugging for other projects.
First, I needed to see where things were going wrong, so I just put some alert boxes to see what part of the code was not getting exectued. First I put in an alert box telling me the value of the selectedIndex before I even got to the switch statment. That was reading the correct value, 2 when I selected the second item. Next I went to case 2 and commented out your code and put in an alert box to see if I was getting to case 2. Nothing poped up, so I never got to case 2. Then I put in a default case that just said "I am in default" and tested again, and that message popped up, so I knew none of the cases were being read, but I was in the switch statement. Since I knew the value of the switch case was 2, but it wasn't going to case 2, I changed case 2 to read case "2" and then everything worked.
So basically, returning the value of the selectedIndex is a string and not an integer. But you were doing you case as integers. In Netscape it converted it for you, but in IE it didn't so you need to convert it to an int (can't remember off hand how to do that in Javascript because I have been working in ASP for the past few months) or you can just change all of your case statmements to read case "2", case "3" etc...
Hope this helps.
Bill
 
Rajpal Kandhari
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bill,
Thanks for the reply. I work only on IE so I could not test the difference. And I really appreciate that you took pain in suggesting me how can I use alert boxes to debug my code. Moreover you took time in writing, Thanks for every thing.
You r right the value returned to the function is a String. So we know where we were going wrong. Bill, do you have any suggestion on how to improve the code that I have written? Because this thing I am going to use in my project. I will be making a servlet performing above function.
Later I changed the content of switch statement that is "switch (thisForm.from.value)" and volla it also worked. Pls. try it out and see � I think it should also work on both the browsers. Waiting for you're comments and suggestions,

Regards
Raj.
 
Don't touch me. And dont' touch this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!