View Single Post
Old 03-27-2003, 09:42 AM   #1 (permalink)
UnderWing
Registered User
 
Join Date: Mar 2003
Posts: 2
UnderWing is on a distinguished road
Just a newbie asking about JS

I'm a complete newbie to Javascript, so here's my problem:

I'm making a calculator program (code following) that is simple and can only compute Addition, Subtraction, Multiplication, Division, and Powers. I use radio buttons to have the user input his/her type of equation, and two text fields to input the numbers involved. When they press a button, the page returns an alert window with the solution. I have gotten everything to work, except addition. It seems to want to treat the input as a string, instead of a variable. Here's the code:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bg="#ffffff" text="#000000">
<script language="JavaScript" type="text/javascript">
<!--
function solve()
{
var f = document.f;
var num1 = 0;
var num2 = 0;
var sign = 0;
var final = 0;
var i = 0;


/*check for num1.*/
num1 = f.num1.value;

/*check for sign.*/ /*signs: 1 = +, 2 = -, 3 = *, 4 = /, 5 = ^*/
for (i = 0; i < f.sign.length; i++) if (f.sign[i].checked) sign = i + 1;

/*check for num2.*/
num2 = f.num2.value;

if (sign == 1) final = (num1 + num2);
if (sign == 2) final = num1 - num2;
if (sign == 3) final = num1 * num2;
if (sign == 4) final = num1 / num2;
if (sign == 5) {
var power = num2;
final = num1;
while (power > 1) {
final = final * num1;
power--
}
}

alert(final);



}
</script>

<form name="f">
<input name="num1" value=0>
<br>
<input type="radio" name="sign" value="1"> +<br>
<input type="radio" name="sign" value="2"> -<br>
<input type="radio" name="sign" value="3"> *<br>
<input type="radio" name="sign" value="4"> /<br>
<input type="radio" name="sign" value="5"> ^<br>
<input name="num2" value=0>
<br>
<input type="button" value="Solve" onclick="solve(f);">
</form>
</body>
</html>
UnderWing is offline   Reply With Quote