www.webdeveloper.com
Results 1 to 7 of 7

Thread: Javascript math with variables

  1. #1
    Join Date
    Mar 2014
    Posts
    4

    Post Javascript math with variables

    I have just started with java script, but already, I have ran into some noob related issues. I am trying to make a simple program to solve for X in a quadratic equation. I have three prompts which ask for the variables of a, b, and c, and then it is supposed to use the quadratic formula to solve for X. However, whenever I try to run it, it will give me the prompts and then not do anything. Here is my script.
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var A = prompt("what is variable a?")
    var B = prompt("what is variable b?")
    var C = prompt("what is variable c?")
    var BA = math.pow(B,2)
    var Answer1 = (4*A*C-1+BA)
    var Answer2 = (2*A)
    var Answer3 = (Answer1/Answer2)
    alert(Answer3)
    </script>
    </body>
    </html>
    Right now, it is just solving for the discriminant, and I was going to continue, but i got this error and was stuck. Could anyone tell me what I'm doing wrong? I'm just using notepad.

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,373
    Convert your strings to numbers.
    Avoid 'prompt's in the future or give some default values.
    There is not such command as math. It is Math.
    Use the error console if using the FF or Chrome browsers.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    // From: http://www.webdeveloper.com/forum/showthread.php?292299-Javascript-math-with-variables
    
    var A = Number(prompt("what is variable a?",1));
    var B = Number(prompt("what is variable b?",2));
    var C = Number(prompt("what is variable c?",3));
    var BA = Math.pow(B,2);
    var Answer1 = (4*A*C-1+BA);
    var Answer2 = (2*A);
    var Answer3 = (Answer1/Answer2);
    alert(Answer3);
    </script>
    </body>
    </html>
    Good Luck!

  3. #3
    Join Date
    Mar 2014
    Posts
    4
    Quote Originally Posted by JMRKER View Post
    Convert your strings to numbers.
    Avoid 'prompt's in the future or give some default values.
    There is not such command as math. It is Math.
    Use the error console if using the FF or Chrome browsers.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    // From: http://www.webdeveloper.com/forum/showthread.php?292299-Javascript-math-with-variables
    
    var A = Number(prompt("what is variable a?",1));
    var B = Number(prompt("what is variable b?",2));
    var C = Number(prompt("what is variable c?",3));
    var BA = Math.pow(B,2);
    var Answer1 = (4*A*C-1+BA);
    var Answer2 = (2*A);
    var Answer3 = (Answer1/Answer2);
    alert(Answer3);
    </script>
    </body>
    </html>
    Good Luck!


    Thanks for the help!!! But I seem to have encountered a similar problem. Again, the prompts won't even show up. I don't think I made the same mistake... But again, I am a noob at this. Here is the updated script:
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var A = Number(prompt("what is variable a?",0));
    var B = Number(prompt("what is variable b?",0));
    var C = Number(prompt("what is variable c?",0));
    var BA = Math.pow(B,2);
    var Answer1 = (4*A*C-1+BA);
    var Answer2 = (2*A);
    var Answer3 = math.sqrt(Answer1);
    var Answer4 = (Answer3/Answer2);
    var Answer5 = (-B/Answer2);
    var Answer6 = (Answer5+Answer4);
    var Answer7= = (Answer5-Answer4);
    (alert(Answer6 "or" Answer7))
    </script>
    </body>
    </html>

  4. #4
    Join Date
    Oct 2013
    Posts
    470
    See lines in red and compare to your code.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var A = Number(prompt("what is variable a?",0));
    var B = Number(prompt("what is variable b?",0));
    var C = Number(prompt("what is variable c?",0));
    var BA = Math.pow(B,2);
    var Answer1 = (4*A*C-1+BA);
    var Answer2 = (2*A);
    var Answer3 = Math.sqrt(Answer1);
    var Answer4 = (Answer3/Answer2);
    var Answer5 = (-B/Answer2);
    var Answer6 = (Answer5+Answer4);
    var Answer7 = (Answer5-Answer4);
    alert(Answer6 + " or " + Answer7);
    </script>
    </body>
    </html>
    Last edited by Kevin2; 03-26-2014 at 09:27 AM.

  5. #5
    Join Date
    Mar 2014
    Posts
    4

    resolved

    Quote Originally Posted by Kevin2 View Post
    See lines in red and compare to your code.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    var A = Number(prompt("what is variable a?",0));
    var B = Number(prompt("what is variable b?",0));
    var C = Number(prompt("what is variable c?",0));
    var BA = Math.pow(B,2);
    var Answer1 = (4*A*C-1+BA);
    var Answer2 = (2*A);
    var Answer3 = Math.sqrt(Answer1);
    var Answer4 = (Answer3/Answer2);
    var Answer5 = (-B/Answer2);
    var Answer6 = (Answer5+Answer4);
    var Answer7 = (Answer5-Answer4);
    alert(Answer6 + " or " + Answer7);
    </script>
    </body>
    </html>
    Thanks so much! I'll try to look more closely at my code in the future.

  6. #6
    Join Date
    Oct 2013
    Posts
    470

    Talking Just for fun

    Because:
    a) I remember a horribly frustrating exercise to find the real and non-real roots of the quadratic formula in a FORTRAN class in college a millenium ago.
    b) I detest, hate, loathe and despise JavaScript Prompts and Alerts

    I placed your math in a new "wrapper":
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Quadratic Equation</title>
    <style>
    .alert{
       color:red;
    }
    </style>
    <script>
    function q(a,b,c,s,r1,r2,rb,n,z,aa,ab,ac){
        var A = a.value,
    	B = b.value,
    	C = c.value;
        if ((A != '') && (B != '') && (C != '')){
    	var BA = Math.pow(B,2),
    	    Answer1 = (4*A*C-1+BA),
    	    Answer2 = (2*A),
    	    Answer3 = Math.sqrt(Answer1),
    	    Answer4 = (Answer3/Answer2),
    	    Answer5 = (-B/Answer2),
    	    Answer6 = (Answer5+Answer4),
    	    Answer7 = (Answer5-Answer4);
    	r1.innerHTML = Answer6;
    	r2.innerHTML = Answer7;
    	if ((r1.innerHTML == 'NaN') || (r2.innerHTML == 'NaN')){  // non-real roots
    	    n.style.display = 'block';
    	    rb.style.display = 'block';
    	}else{
    	    s.style.display = 'block';
    	    rb.style.display = 'block';
    	}
        }else{
    	if (A == ''){
    	    aa.style.display = 'inline';
    	    z.style.display = 'none';
    	}
    	if (B == ''){
    	    ab.style.display = 'inline';
    	}
    	if (C == ''){
    	    ac.style.display = 'inline';
    	}
        }
    }
    
    function valid(el,alert,n,s,rb,z){
        if ((el = a) && (el.value == '0')){  // prevents dividing by zero
    	z.style.display = 'inline';
    	alert.style.display = 'none';
    	el.value = '';
    	el.focus();
        }else{
    	z.style.display = 'none';
    	alert.style.display = 'none';
    	n.style.display = 'none';
    	s.style.display = 'none';
    	rb.style.display = 'none';
        }
    }
    </script>
    </head>
    <body>
    
    <h1>The Quadratic Equation</h1>
    
    <p>Solves for real roots only.</p>
    
    <p><label for="a">Enter a numeric value for variable a:</label> <input type="number" id="a" placeholder="2" onblur="valid(this,alerta,nan,show,reset,zero)" /> <span class="alert" id="alerta" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span><span class="alert" id="zero" style="display:none;">&lt;&mdash;&mdash; Variable 'a' cannot equal 0 (zero).</span><br />
       <label for="b">Enter a numeric value for variable b:</label> <input type="number" id="b" placeholder="4" onblur="valid(this,alertb,nan,show,reset,zero)" /> <span class="alert" id="alertb" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span><br />
       <label for="c">Enter a numeric value for variable c:</label> <input type="number" id="c" placeholder="6" onblur="valid(this,alertc,nan,show,reset,zero)" /> <span class="alert" id="alertc" style="display:none;">&lt;&mdash;&mdash; Missing or non-numeric value!</span></p>
    
    <p><input type="button" value="Solve" onclick="q(a,b,c,show,result1,result2,reset,nan,zero,alerta,alertb,alertc)" /></p>
    
    <p id="show" style="display:none;">Results:<br />
       <b><span id="result1"></span></b><br />
       &nbsp; &nbsp; &ndash; OR &ndash;<br />
       <b><span id="result2"></span></b></p>
    
    <p id="nan" style="display:none;">The values entered yielded non-real roots.</p>
    
    <p id="reset" style="display:none;"><input type="button" value="Reset" onclick="window.location.reload()" /></p>
    
    </body>
    </html>

  7. #7
    Join Date
    Mar 2014
    Posts
    4
    Thank you! That was very helpful, and a big improvement. Sorry it took so long to reply. Thanks for your help everyone!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles