View Full Version : here? or there?


Dolan
05-29-2003, 07:53 PM
Hello. What's wrong with this:

<form name="ms">
Level: <input type="text" name="input" value="" size="10">
<input type="button" onClick="scalc(ms.input.value);" value="Calculate!">
Defense in %: <input type="text" name="output" size="35">
</form>
<script type="text/javascript">
function scalc(w) {
if (w == "0") {
ms.output.value = "Hey, you can't expect something from 0";}
if (w > "0") {
ms.output.value = Math.roof(w/9)*0.455+0.114;}
}
</script>

It only prints when input is 0. So I guess there's something wrong with the math expression.
Thanx for a good page that have helped me alot.

Dude128
05-30-2003, 03:37 AM
I'm not *too* familiar with exact JavaScript syntax, but my guess is that it's with your (w > "0") condition.

I'm not sure that's valid, to compare numeric input against a string- I know that in Java you can't use comparison operators with strings, you have to use special functions instead. it could be similar in JavaScript.

one way to get around it is to assume the user is entering a positive number or take the absolute value of the input by simply using (w != "0")

EDIT: or you could just replace your second if (...) with else, because that will do the same thing :) either way, you should probably have an else before the if at least, so that if the input is 0, the second condition isn't even checked.

another potential problem is using a string in not only your condition but in the calculation- there might be some way to convert a string to an integer, which you could then use instead of the above in your condition and in your calculation.

another possible issue: you divide the input by 9, and then round- but if the input is an integer, your result will be truncated before it can be rounded. it might be better to use Math.roof(w/9.) so that you'll end up with a floating point number.