Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem with If Else...

1. ## Problem with If Else...

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!

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;(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>```

• 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...

• ## Users who have thanked xelawho for this post:

lilwillywonka (09-05-2011)

• 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.

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>```

• If and Else should not be capitalized.

• hmmm... without even knowing what a quadratic equation is, it would seem to me that this code:

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

• Originally Posted by lilwillywonka
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
Code:
```<?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>```

• Code:
`else if (form.Dis.value = 0)`
In javascript = is the assignment operator, not the comparison operator. The latest is ==.
Code:
`else if (form.Dis.value == 0)`

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•