PDA

View Full Version : Trouble with Trig generator

RGStrat
03-01-2009, 09:56 PM
Hi, let me start off by saying I am a total javascript newbie. I was doing trig homework one afternoon and thought it would be fun to try and make a calculator for it. To put it mildly, I failed :) . I tried to figure it out myself but I can't. Can someone help me?

<FORM name="Calculator">
<div style="float:left; width:150px;text-align:right;">Radius: <br />Number of Sides: </div>
<div style="float:right; width:150px;text-align:left;">
<input style="width:145px;margin-left:5px;" type="text" maxlength="40" id="Sides" name="Sides" /><br />
</div>
<input type="button" value="Solve" OnClick="FindArea" />
</FORM>
<script type="text/javascript">
function FindArea(cb) {
if (document.getElementById) {
var elSides = document.getElementById('Sides');
var elFirstDegree = 360 / elSides;
var elSecondDegree = elFirstDegree * .5;
var elApothem = elRadius * cos(elSecondDegree);
var elX = elRadius * sin(elSecondDegree);
var elParimeter = elX * 2 * elSides;
}
document.write(Math..5 * elApothem * elPerimeter)
}
</script>

Any help would be appreciated!

Thanks,

James

RGStrat
03-09-2009, 11:41 PM
Bump?

jscheuer1
03-10-2009, 03:46 PM
I wouldn't know an Apothem if it bit me, but this is as close as I can get in deciphering your intent:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
form {
width: 20em;
}
.text0 {
float: left;
width: 9.375em;
text-align:right;
}
.text1 {
float: right;
width: 9.375em;
text-align:left;
}
.text1 input {
width: 9.0625em;
margin-left: 0.625ex;
}
.text3 {
clear: both;
}
</style>
<script type="text/javascript">
function FindArea(f) {
f = f.elements;
var elSides = f.Sides.value;
var elFirstDegree = 360 / elSides;
var elSecondDegree = elFirstDegree * .5;
var elApothem = elRadius * Math.cos(elSecondDegree);
var elX = elRadius * Math.sin(elSecondDegree);
var elPerimeter = elX * 2 * elSides;
f.result.value = .5 * elApothem * elPerimeter;
};
</script>
<body>
<form action="#" name="Calculator" onsubmit="FindArea(this);return false;">
<div class="text0">Radius: <br>Number of Sides: </div>
<div class="text1">