Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

0s and 1s

macrumors 6502a
Original poster
Long time reader, first time Web D&D poster here...

I'm having issues with my JS project. My teacher is horrible and the book used in my class just plain sucks. Not only that, but the decision to learn JS was decided only 2 weeks ago, with a week left before finals! Pardon my inexperience, but I need help! The point of this project is to enter 3 sets of coordinates and display the slope and distances of said coordinates. Upon execution, the type of triangle and explanation of the triangle will be displayed. For example, if the sides are 5, 5, 5, It would display Equilateral Triangle and ...all sides are equal in triangle... as the explanation. Should I incorporate arrays to hold triangle type and explanation info? If so, would I use them in an if...else statement? Last, when I entered the coordinates, the slope and distance doesn't show up? What's wrong with my code? I believe it has something to do with the parentheses. Any help would be appreciated. Thanks a lot!
 
edit: I think it would be beneficial if you'd be able to see the code! 😛

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Project: Triangle Calculations Javascript</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<SCRIPT TYPE= "text/javascript">
<!--
function process()
{
//array to be used later.
var tType = new Array();
tType[0] = "This is an equilateral triangle.";
tType[1] = "This is an isoceles triangle!";
tType[2] = "This is a scalene triangle!";

var tExplain = new Array();
tExplain[0] = "blah";
tExplain[1] = "blah";
tExplain[2] = "and more blah";

var x1Input = myForm.x1.value;
var x2Input = myForm.x2.value;
var x3Input = myForm.x3.value;
var y1Input = myForm.y1.value;
var y2Input = myForm.y2.value;
var y3Input = myForm.y3.value;

var slo1 = (y1Input - y2Input) / (x1Input - x2Input);
var slo2 = (y1Input - y3Input) / (x1Input - x3Input);
var slo3 = (y2Input - y3Input) / (x2Input - x3Input);

var dis1 = Math.sqrt((x2Input - x1Input)(x2Input - x1Input) + (y2Input - y1Input)(y2Input - y1Input));
var dis2 = Math.sqrt((x3Input - x1Input)(x3Input - x1Input) + (y3Input - y1Input)(y3Input - y1Input));
var dis3 = Math.sqrt((x3Input - x2Input)(x3Input - x2Input) + (y3Input - y2Input)(y3Input - y2Input));

myForm.slope1.value = slo1;
myForm.slope2.value = slo2;
myForm.slope3.value = slo3;
myForm.distance1.value = dis1;
myForm.distance2.value = dis2;
myForm.distance3.value = dis3;

//work to be done later....
myForm.triExplain.value = tExplain;
myForm.triType.value = tType;
}

/-->

</SCRIPT>

</head>
<body>
<!--<BODY BACKGROUND="http://www.gtcocalcomp.com/InterWriteBackgrounds/graph_paper.jpg"> /-->


<FORM NAME="myForm" ACTION="" METHOD="GET">
<CENTER><FONT COLOR="black"><H3>Testing Triangles</H3></FONT></CENTER>
<HR WIDTH=35% COLOR="blue">

<BR><BR>

<LABEL><STRONG>Please enter the first coordinate (</STRONG></LABEL>
<INPUT TYPE="TEXT" NAME="x1" SIZE=5><LABEL><STRONG>,</STRONG></LABEL><INPUT TYPE="TEXT" NAME="y1" SIZE=5><LABEL><STRONG>)</STRONG></LABEL>
<BR>
<LABEL><STRONG>Please enter the second coordinate (</STRONG></LABEL>
<INPUT TYPE="TEXT" NAME="x2" SIZE=5><LABEL><STRONG>,</STRONG></LABEL><INPUT TYPE="TEXT" NAME="y2" SIZE=5><LABEL><STRONG>)</STRONG></LABEL>
<BR>
<LABEL><STRONG>Please enter the third coordinate (</STRONG></LABEL>
<INPUT TYPE="TEXT" NAME="x3" SIZE=5><LABEL><STRONG>,</STRONG></LABEL><INPUT TYPE="TEXT" NAME="y3" SIZE=5><LABEL><STRONG>)</STRONG></LABEL>

<BR><BR>

<TABLE WIDTH="50%" BGCOLOR="gray" BORDER=2 BORDERCOLOR=black align="center">
<TR BGCOLOR="yellow" align="center"><TD WIDTH="35%"><STRONG>Slope</STRONG></TD><TD WIDTH="5%"> </TD><TD WIDTH="25%"><STRONG>Distance
</STRONG></TD>
</TR>
<TR>
<TD WIDTH="35%" ALIGN="CENTER">(X<SUB>1</SUB>,Y<SUB>1</SUB>), (X<SUB>2</SUB>,Y<SUB>2</SUB>) = <input type="text" name="slope1" size="10" value=""></TD>
<TD WIDTH="5%"></TD>
<TD WIDTH="25%" ALIGN="CENTER"><input type="text" name="distance1" size="10" value=""></TD>
</TR>
<TR>
<TD WIDTH="35%" ALIGN="CENTER">(X<SUB>1</SUB>,Y<SUB>1</SUB>), (X<SUB>3</SUB>,Y<SUB>3</SUB>) = <input type="text" name="slope2" size="10" value=""></TD>
<TD WIDTH="5%"></TD>
<TD WIDTH="25%" ALIGN="CENTER"><input type="text" name="distance2" SIZE="10" value=""></TD>
</TR>
<TR>
<TD WIDTH="35%" ALIGN="CENTER">(X<SUB>2</SUB>,Y<SUB>2</SUB>), (X<SUB>3</SUB>,Y<SUB>3</SUB>) = <input type="text" name="slope3" size="10" value=""></TD>
<TD WIDTH="5%"></TD>
<TD WIDTH="25%" ALIGN="CENTER"><input type="text" name="distance3" size="10" value=""></TD>
</TR>

</TABLE>
</CENTER>

<BR><BR>

<LABEL><STRONG>Triangle type:</STRONG></LABEL><BR>
<INPUT TYPE="TEXT" NAME="triType" SIZE=50>
<BR><BR>
<LABEL><STRONG>Explanation of triangle:</STRONG></LABEL><BR>
<TEXTAREA NAME="triExplain" WRAP=VIRTUAL ROWS=9 COLS=50></TEXTAREA>  

<BUTTON name="Process" onclick="process()";>Test Triangle</BUTTON>
<BUTTON TYPE=RESET value="Clear All">Clear All </BUTTON>


</FORM>
</BODY>
</HTML>
 
updated it with the addition of arrays, but it still doesn't work! Also, my teacher wants the slope displayed as a fraction, but I could only think of doing it by concatenating with + "/" +, which makes it difficult when both numbers are - or the denom. is 0. Is there any chance of displaying it as a fraction and at the same time deal with the 0 as the denom. and having both numbers negative? Thanks a lot!!!!!!!!!!!!!!!!!!!! 🙁

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Project: Triangle Calculations Javascript</title>

<SCRIPT TYPE= "text/javascript">
<!--
function process()
{

var x = new Array();
x[0] = myForm.x1.value;
x[1] = myForm.x2.value;
x[2] = myForm.x3.value;

var y = new Array();
y[0] = myForm.y1.value;
y[1] = myForm.y2.value;
y[2] = myForm.y3.value;

var tType = new Array();
tType[0] = "This is an equilateral triangle.";
tType[1] = "This is an isoceles triangle!";
tType[2] = "This is a scalene triangle!";

var tExplain = new Array();
tExplain[0] = "blah";
tExplain[1] = "blah";
tExplain[2] = "and more blah";

var slo1 = Math.round(y[1] - y[0]) + "/" + Math.round(x[1] - x[0]);
var slo2 = Math.round(y[0] - y[2]) + "/" + Math.round(x[0] - x[2]);
var slo3 = Math.round(y[1] - y[2]) + "/" + Math.round(x[1] - x[2]);

var dis1 = Math.round(Math.sqrt((x[1] - x[0])*(x[1] - x[0]) + (y[1] - y[0])*(y[1] - y[0])));
var dis2 = Math.round(Math.sqrt((x[2] - x[0])*(x[2] - x[0]) + (y[2] - y[0])*(y[2] - y[0])));
var dis3 = Math.round(Math.sqrt((x[2] - x[1])*(x[2] - x[1]) + (y[2] - y[1])*(y[2] - y[1])));

if ( dis1 == dis2 && dis1 == dis3 )
{
myForm.tType.value = tType[0];
else if ( dis1 == dis2 || dis1 == dis3 )
myForm.tType.value = tType[1];
else
myForm.tType.value = tType[2];
}

if ( myForm.tType.value = tType[0] )
{
myForm.tExplain.value = tExplain[0];
else if ( myForm.tType.value = tType[1] )
myForm.tExplain.value = tExplain [1];
else
myForm.tExplain.value = tExplain[2];
}

myForm.slope1.value = slo1;
myForm.slope2.value = slo2;
myForm.slope3.value = slo3;
myForm.distance1.value = dis1;
myForm.distance2.value = dis2;
myForm.distance3.value = dis3;

}
//-->

</SCRIPT>

</head>
<body>

<FORM NAME="myForm" ACTION="" METHOD="GET">
<CENTER><FONT COLOR="black"><H3>Testing Triangles</H3></FONT></CENTER>
<HR WIDTH=35% COLOR="blue">

<BR><BR>

<LABEL><B>Please enter the first coordinate (</B></LABEL>
<INPUT TYPE="TEXT" NAME="x1" SIZE=5><LABEL><B>,</B></LABEL><INPUT TYPE="TEXT" NAME="y1" SIZE=5><LABEL><B>)</B></LABEL>
<BR>
<LABEL><B>Please enter the second coordinate ( </B> </LABEL>
<INPUT TYPE="TEXT" NAME="x2" SIZE=5><LABEL><B>,</B></LABEL><INPUT TYPE="TEXT" NAME="y2" SIZE=5><LABEL><B>)</B></LABEL>
<BR>
<LABEL><B>Please enter the third coordinate (</B></LABEL>
<INPUT TYPE="TEXT" NAME="x3" SIZE=5><LABEL><B>,</B></LABEL><INPUT TYPE="TEXT" NAME="y3" SIZE=5><LABEL><B>)</B></LABEL>

<BR><BR>

<TABLE WIDTH="55%" BGCOLOR="gray" BORDER=2 BORDERCOLOR=black align="CENTER">

<TR>
<TD WIDTH="35%" align="center"><B>Slope</B></TD><TD WIDTH="5%" bgcolor="red"> </TD><TD WIDTH="25%" align="center"><B>Distance</B>
</TD>
</TR>

<TR>
<TD WIDTH="35%" align="CENTER">(X<SUB>1</SUB>,Y<SUB>1</SUB>), (X<SUB>2</SUB>,Y<SUB>2</SUB>) = <input type="text" name="slope1" size=10>
</TD>
<TD WIDTH="5%" bgcolor="red"> </TD>
<TD WIDTH="25%" align="CENTER"><input type="text" name="distance1" size=10></TD>
</TR>

<TR>
<TD WIDTH="35%" align="CENTER">(X<SUB>1</SUB>,Y<SUB>1</SUB>), (X<SUB>3</SUB>,Y<SUB>3</SUB>) = <input type="text" name="slope2" size=10>
</TD>
<TD WIDTH="5%" bgcolor="red"> </TD>
<TD WIDTH="25%" align="CENTER"><input type="text" name="distance2" size=10></TD>
</TR>

<TR>
<TD WIDTH="35%" align="CENTER">(X<SUB>2</SUB>,Y<SUB>2</SUB>), (X<SUB>3</SUB>,Y<SUB>3</SUB>) = <input type="text" name="slope3" size=10>
</TD>
<TD WIDTH="5%" bgcolor="red"> </TD>
<TD WIDTH="25%" align="CENTER"><input type="text" name="distance3" size=10></TD>
</TR>

</TABLE>
</CENTER>

<BR><BR>

<LABEL><B>Triangle type:</B></LABEL><BR>
<INPUT TYPE="TEXT" NAME="triType" SIZE=50>
<BR><BR>
<LABEL><B>Explanation of triangle:</B></LABEL><BR>
<TEXTAREA NAME="triExplain" WRAP=VIRTUAL ROWS=9 COLS=50></TEXTAREA>  

<BUTTON name="Process" onclick="process()";>Test Triangle</BUTTON>
<BUTTON TYPE=RESET value="Clear All">Clear All </BUTTON>


</FORM>
</BODY>
</HTML>
 
James L said:
Go here:

http://www.webdeveloper.com/forum/

And surf to the javascript forum. Ask your question there, the guys are awesome about helping out when they can.

Cheers,

James

James:
thanks a lot! That site is amazing. I'm in programming heaven. I just posted the code there.....here's hoping I get some help....

once again, thank you!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.