Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Im trying to learn please help. 1

Status
Not open for further replies.

mertcek123

Programmer
Nov 19, 2010
4
IT
Hello my friends,

Im trying to learn javascript I wrote this code, this is my first trial. But it doesnt work can you help me to solve the problem. I think you will understand it what does it work for.. like in highschool it finds the value of discriminant and says whether the roots are real or not.

this my code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="Javascript">

function delta ()

{ var a = first.A.value;
var b = second.B.value;
var c = third.C.value;
var delta = (b*b) - (4*a*c);

if (delta<0)
{
alert("Equation has no real roots");
}
else if (delta=0)
{
alert("Equation has 2 real equal roots");
}
else if (delta >0)
{
alert("Equation has 2 different real roots");
}

}
</script>
</head>

<body>
<FORM NAME="first">
A'yi yaziniz: <INPUT TYPE="text" NAME=A SIZE=5>
</INPUT>

<FORM NAME="second">
B'yi yaziniz: <INPUT TYPE="text" NAME=B SIZE=5>
</INPUT>

<FORM NAME="third">
C'yi yaziniz: <INPUT TYPE="text" NAME=C SIZE=5>
</INPUT>

<input name="execute" type="button" value="Diskriminant'i gor" onclick="delta()" />
</body>
</html>

please help me to improve myself.
 
Not sure if you are doing this from a book, but there are several things that are wrong and would stop execution.

Also I would encourage you to use your browser's error console, it will help you debug.

Now for the problems:

The first one would be the wrong type in the script tags:
Code:
<script type="Javascript">
It should be:
Code:
<script type="[red]text/j[/red]avascript">

Otherwise it would automatically cause any code inside the script tags to be ignored. So you would get an undefined function error.

Second:
Code:
  var a = first.A.value;
    var b = second.B.value;
    var c = third.C.value;
Accessing the text inputs like that would produce "undefined variable" errors. You need to use the full path so to speak.

Code:
  var a = [red]document.[/red]first.A.value;
    var b = [red]document.[/red]second.B.value;
    var c = [red]document.[/red]third.C.value;

Third your second comparison:
Code:
else if (delta[red]=[/red]0)
will always be true, because a single "=" sign is for assignment in Javascript, and assignment will always succeed, that is you can always make delta equal to 0 and it will be true. You need two "==" signs for comparison.

Code:
else if (delta=[red]=[/red]0)


Fourth, and this will cause issues with the second point above regardless of what you do: is you never close your forms:

Code:
<FORM NAME="first">
A'yi yaziniz: <INPUT TYPE="text" NAME=A SIZE=5>
</INPUT>
[red]>><<[/red]
<FORM NAME="second">
B'yi yaziniz: <INPUT TYPE="text" NAME=B SIZE=5>
</INPUT>
[red]>><<[/red]
<FORM NAME="third">
C'yi yaziniz: <INPUT TYPE="text" NAME=C SIZE=5>
</INPUT>
[red]>><<[/red]

What that means is instead of having 3 separate forms you have 3 nested forms (each inside the preceding one), so you can't access the second and third directly as document.second or document.third because since they are now nested inside the preceding form they end up having to be accessed through the parent form such as: document.first.second, and document.first.second.third.

Closing the forms will solve that.

Code:
<FORM NAME="first">
A'yi yaziniz: <INPUT TYPE="text" NAME=A SIZE=5>
</INPUT>
[red][/FORM][/red]
<FORM NAME="second">
B'yi yaziniz: <INPUT TYPE="text" NAME=B SIZE=5>
</INPUT>
[red][/FORM][/red]
<FORM NAME="third">
C'yi yaziniz: <INPUT TYPE="text" NAME=C SIZE=5>
</INPUT>
[red][/FORM][/red]

Of course this brings up a question: Why so many forms? You can use a single form and still access each input separately as:

document.[green]formname[/green].[blue]elementname[/blue].value



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thank you very much for your help my friend 'vacunita' .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top