Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 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
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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>

  • #4
    New Coder
    Join Date
    Aug 2011
    Posts
    51
    Thanks
    6
    Thanked 12 Times in 12 Posts
    If and Else should not be capitalized.

    Edit: whoop, someone already answered. x.x

  • #5
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    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

  • #6
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote 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>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #7
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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)
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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