View Full Version : Trouble with Trig generator

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="Radius" name="Radius" /><br />
<input style="width:145px;margin-left:5px;" type="text" maxlength="40" id="Sides" name="Sides" /><br />
<input type="button" value="Solve" OnClick="FindArea" />
<script type="text/javascript">
function FindArea(cb) {
if (document.getElementById) {
var elRadius = document.getElementById('Radius');
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)

Any help would be appreciated!



03-09-2009, 11:41 PM

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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
form {
width: 20em;
.text0 {
float: left;
width: 9.375em;
.text1 {
float: right;
width: 9.375em;
.text1 input {
width: 9.0625em;
margin-left: 0.625ex;
.text3 {
clear: both;
<script type="text/javascript">
function FindArea(f) {
f = f.elements;
var elRadius = f.Radius.value;
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;
<form action="#" name="Calculator" onsubmit="FindArea(this);return false;">
<div class="text0">Radius: <br>Number of Sides: </div>
<div class="text1">
<input type="text" maxlength="40" name="Radius"><br>
<input type="text" maxlength="40" name="Sides"><br>
<div class="text3">
<input type="submit" value="Solve"><br>
Result: <input type="text" name="result">