PDA

View Full Version : Problem with If Else...

lilwillywonka
09-05-2011, 04:10 AM
This is a program that calculates discriminant/number of solutions for a given quadratic equation.. it all works, but the number of solution field is not filled, so i think i did something wrong on my if else statements for it.. help!

<html>
<title>Untitled</title>
<script type="text/javascript">
<!-- Begin
a = form.a.value
b = form.b.value
c = form.c.value
form.Dis.value = (b * b) - (4 * a * c)
If(form.Dis.value > 0)
{
form.NumSol.value = 2
}
Else;(form.Dis.value = 0)
{
form.NumSol.value = 1
}
Else
{
form.NumSol.value = 0
}

}
// End -->
</script>

<body><center>
<form name="form1">
<input type="text" name="a" size=5>
x^2 +

<input type="text" name="b" size=5>
x +

<input type="text" name="c" size=5>

<input type="button" value="Calculate"

Discriminant: <input type="text" name="Dis" size = 7 />
Number of Solutions: <input type="text" name="NumSol" size = 7 />

</form>
</center>

</body>
</html>

xelawho
09-05-2011, 04:38 AM
two things I can see:

1) if and else have to be written like that, in lower case. If and Else doesn't cut it.

2) this:
Else;(form.Dis.value = 0)

should be
else if (form.Dis.value = 0)

that may be a start, anyway...

lilwillywonka
09-05-2011, 04:42 AM
thank you.. it works now.. but for some reason, the discriminant field will not display negative numbers. the number of solutions works normally but the discriminant is either positive or zero. this is my updated code.

<html>
<title>Untitled</title>
<script type="text/javascript">
<!-- Begin
{
a = form.a.value
b = form.b.value
c = form.c.value
form.Dis.value = (b * b) - (4 * a * c)
if (form.Dis.value > 0)
{
form.NumSol.value = 2;
}
else if (form.Dis.value = 0)
{
form.NumSol.value = 1;
}
else
{
form.NumSol.value = 0;
}

}
// End -->
</script>

<body><center>
<form name="form1">
<input type="text" name="a" size=5>
x^2 +

<input type="text" name="b" size=5>
x +

<input type="text" name="c" size=5>

<input type="button" value="Calculate"

Discriminant: <input type="text" name="Dis" size = 7 />
Number of Solutions: <input type="text" name="NumSol" size = 7 />

</form>
</center>

</body>
</html>

ASTP001
09-05-2011, 04:45 AM
If and Else should not be capitalized.

xelawho
09-05-2011, 04:56 AM
hmmm... without even knowing what a quadratic equation is, it would seem to me that this code:

if (form.Dis.value > 0)
{
form.NumSol.value = 2;
}
else if (form.Dis.value = 0)
{
form.NumSol.value = 1;
}
else
{
form.NumSol.value = 0;
}

doesn't actually allow for negative numbers, so I'm assuming that if form.Dis.value is negative, form.NumSol.value will be 0

but then (still without really understanding what's going on) you do understand the difference between = and == in javascript, right?
http://www.w3schools.com/js/js_comparisons.asp

Arbitrator
09-05-2011, 12:03 PM
thank you.. it works now.. but for some reason, the discriminant field will not display negative numbers. the number of solutions works normally but the discriminant is either positive or zero. this is my updated code.Your second if statement's condition (i.e., form.Dis.value = 0) will cause form.Dis.value to be assigned a value of 0. This is because you used an assignment operator (i.e., =) when you apparently meant to use an equality operator (i.e., ==).

Since the first if statement's condition (i.e., form.Dis.value > 0) evaluates to false when the discriminant is negative, the second if statement will always be checked in such cases, and, therefore, a negative discriminant will always be replaced with a value of 0.

I rewrote your code for practice in case you're interested.

(It seems to work fine in Firefox 7 Beta 2, Internet Explorer 9, and Opera 11.5.

Chrome 14 Beta and Safari 5.1 won't display some of the mathematical characters, unfortunately; those are marked with comments in the code. That's odd since those characters have been part of Unicode for ten years now.)

document_1.xhtml

<?xml version="1.0"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
<title>Demo Document</title>
<style>
/* Basic Layout */
* { margin: 0; }
label { display: inline-table; }
label input { width: 5ch; }
label span { display: table-row; }
button { display: block; }
dl { margin: 1em 0; }
dl * { display: inline; }

/* Typography */
* { font-family: serif; }
label { vertical-align: top; }
label * { text-align: center; }
dt { font-weight: bold; }
dt::after { content: ":"; }
dd::after { content: "\A"; white-space: pre; }
</style>
<script>
// <![CDATA[
var d = document;
if (!isNaN(parseFloat(d.getElementById("a").value))
&& !isNaN(parseFloat(d.getElementById("b").value))
&& !isNaN(parseFloat(d.getElementById("c").value))) {
var a = parseFloat(d.getElementById("a").value);
var b = parseFloat(d.getElementById("b").value);
var c = parseFloat(d.getElementById("c").value);
}
else {
d.defaultView.alert("You must enter numbers to represent " +
"𝑎" /* U+1D44E MATHEMATICAL ITALIC SMALL A */ + ", " +
"𝑏" /* U+1D44F MATHEMATICAL ITALIC SMALL B */ + ", and " +
"𝑐" /* U+1D450 MATHEMATICAL ITALIC SMALL C */ + " first!");
}
"𝑥" /* U+1D465 MATHEMATICAL ITALIC SMALL X */ + "² + " + b.toString() +
"𝑥" /* U+1D465 MATHEMATICAL ITALIC SMALL X */ + " + " + c.toString();
var discriminant = Math.pow(b, 2) - (4 * a * c);
var number_of_real_roots;
if (discriminant > 0) {
number_of_real_roots = 2;
}
else if (discriminant === 0) {
number_of_real_roots = 1;
}
else {
number_of_real_roots = 0;
}
d.getElementById("discriminant").firstChild.data = (discriminant.toString() + " = " + b.toString() + "² − (4" + " ∙ " + a.toString() + " ∙ " + b.toString() + ")").replace(/-/, "−");
d.getElementById("number_of_real_roots").firstChild.data = number_of_real_roots.toString();
}
// ]]>
</script>
<body>
<div>
<label for="a"><input id="a" type="number" size="5"></input><span><!---->𝑎<!-- U+1D44E MATHEMATICAL ITALIC SMALL A --></span></label><!---->𝑥<!-- U+1D465 MATHEMATICAL ITALIC SMALL X -->² +
<label for="b"><input id="b" type="number" size="5"></input><span><!---->𝑏<!-- U+1D44F MATHEMATICAL ITALIC SMALL B --></span></label><!---->𝑥<!-- U+1D465 MATHEMATICAL ITALIC SMALL X --> +
<label for="c"><input id="c" type="number" size="5"></input><span><!---->𝑐<!-- U+1D450 MATHEMATICAL ITALIC SMALL C --></span></label>
<dl>
<dt>Discriminant</dt>
<dd id="discriminant">?</dd>
<dt>Number of Real Roots</dt>
<dd id="number_of_real_roots">?</dd>
</dl>
</div>
</body>
</html>

Kor
09-05-2011, 12:06 PM
else if (form.Dis.value = 0)

In javascript = is the assignment operator, not the comparison operator. The latest is ==.

else if (form.Dis.value == 0)