Javascript Query

Giarc86

Expert Member
Joined
May 28, 2008
Messages
1,243
Reaction score
4
I need to write a function that when a user enters a radius of a sphere, the function must calculate and display the volume of the sphere.

I need to use:
volume = (4.0/3.0) * Math.PI * Math.pow(radius, 3);
to calculate it.
I have been trying and googling but just can't seem to get it right.

Thanks
 
DISCLAIMER: I'm not very good at this. Use at your own peril.

Does this help at all?

Code:
<html>
<head>
<script type="text/javascript">
function calc_volume(form)
{
  radius = form.radius.value;
  volume = (4/3) * Math.PI * Math.pow(radius, 3);
  form.volume.value = volume;
}
</script>
</head>

<body>
<form name="myform" action="" method="GET">
Radius: <input type="text" name="radius" value="">
<input type="button" name="calc" value="calculate" onClick="calc_volume(this.form)">
<p>
Volume: <input type="text" name="volume" value="" disabled>
</form>
</body>
</html>
 
Thanks to both :)
That worked, I just couldn't get the function to work correctly, but all good now.

Just curiously how would I go about comparing two Strings that the user would input.
Once compared it would display whether the 1st string is less, equal to or greater than the 2nd string.

I am trying to use GetElementById from the form but it doesn't seem to want to work.
 
You should use...
Code:
document.getElementById("ID of the input field").value
...to access the text that the user typed in.

You should then parse the value entered to int (or float perhaps?) to compare the 2 values.
 
Thanks to both :)
That worked, I just couldn't get the function to work correctly, but all good now.

Just curiously how would I go about comparing two Strings that the user would input.
Once compared it would display whether the 1st string is less, equal to or greater than the 2nd string.

I am trying to use GetElementById from the form but it doesn't seem to want to work.

http://www.google.com/search?hl=en&...pare+two+strings+in+JavaScript+&aq=f&oq=&aqi=

Results 3/4/5 gives you the best explanations, however:

Use what FarligOpptreden showed with a combination of .toLowerCase() to make sure you compare both strings on the same level (JavaScript is case sensitive). If you get stuck go check out my blog www.acidrazor.com for .toLowerCase(), sometimes it gives you a bit of a headache and I have some tips there on how to overcome this.

Then to see which one is longer than the other, you'd have to use .length() to compare it. FarligOpptreden assumed the two strings you will be comparing will be two integer inputs, hence why he recommended parseInt() in that case.

I assume already you know how to use if statements and the like, so above should be pretty straightforward ;)
 
I did say I googled, I couldn't find enough help, the functions that I found were petty things lol

Thank you for the previous post though

Sometimes there's no answers, but websites with answers but hidden in the source ;)
 
FarligOpptreden assumed the two strings you will be comparing will be two integer inputs, hence why he recommended parseInt() in that case.

Yup. Seeing as the OP was concerned with a mathematical calculation, I assumed the user would input integers or floats.
 
Top
Sign up to the MyBroadband newsletter
X