Web Analytics Made Easy -
StatCounter Problem with If Else... - CodingForum

Announcement

Collapse
No announcement yet.

Problem with If Else...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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>
    <head>
      <title>Untitled</title>
    </head>
    <script type="text/javascript">
    <!--    Begin
        function quad(form) {
        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"
    onClick="quad(this.form)">
    
    Discriminant: <input type="text" name="Dis" size = 7 />
    Number of Solutions: <input type="text" name="NumSol" size = 7 />
    
    
    </form>
    </center>
    
    
    
    </body>
    </html>

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

    Comment


    • #3
      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>
      <head>
        <title>Untitled</title>
      </head>
      <script type="text/javascript">
      <!--    Begin
          function quad(form) 
          {
          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"
      onClick="quad(this.form)">
      
      Discriminant: <input type="text" name="Dis" size = 7 />
      Number of Solutions: <input type="text" name="NumSol" size = 7 />
      
      
      </form>
      </center>
      
      
      
      </body>
      </html>

      Comment


      • #4
        If and Else should not be capitalized.

        Edit: whoop, someone already answered. x.x

        Comment


        • #5
          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?
          W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

          Comment


          • #6
            Originally posted by lilwillywonka View Post
            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">
            	<head>
            		<title>Demo Document</title>
            		<style>
            			/* Basic Layout */
            			* { margin: 0; }
            			html { padding: 2em; }
            			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;
            			function analyze_quadratic_polynomial() {
            				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!");
            				}
            				var quadratic_polynomial = a.toString() +
            				"𝑥" /* 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("quadratic_polynomial").firstChild.data = quadratic_polynomial.toString().replace(/-/, "󠀭−");
            				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>
            	</head>
            	<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>
            			<button onclick="analyze_quadratic_polynomial();">Analyze Quadratic Polynomial</button>
            			<dl>
            				<dt>Quadratic Polynomial</dt>
            				<dd id="quadratic_polynomial">?</dd>
            				<dt>Discriminant</dt>
            				<dd id="discriminant">?</dd>
            				<dt>Number of Real Roots</dt>
            				<dd id="number_of_real_roots">?</dd>
            			</dl>
            		</div>
            	</body>
            </html>

            Comment


            • #7
              Code:
              else if (form.Dis.value [COLOR="Red"]=[/COLOR] 0)
              In javascript = is the assignment operator, not the comparison operator. The latest is ==.
              Code:
              else if (form.Dis.value [COLOR="Blue"]==[/COLOR] 0)
              KOR
              Offshore programming
              -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

              Comment

              Working...
              X
              😀
              🥰
              🤢
              😎
              😡
              👍
              👎