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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2008
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Javascript calculator how to use an if else statement

    Hello, I am coding a javascript calculator for a school project and i have a problem with the if..else statment. I want the page to redirect to an other page when a given value(delta) is smaller than 0(zero). I've managed that but when the value(delta) is equal or bigger than 0(zero) it should execute an other function and I don't now how to do that. Here is my code and I have highlited the tricky part. Also, if I have a new function(calculatorx1) but I want the values from an other function(Calculator) on the page do I have to redefine them or not?(italic overline)
    Don't worry about the French in the body and thank you for your time.

    <html>
    <head>
    <title>Calculator</title>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function Calculator(){
    var a=document.calculator.a.value;
    var b=document.calculator.b.value;
    var c=document.calculator.c.value;

    var aq=b*b;
    var aw=4*a*c;
    var delta=aq-aw;

    if
    (delta>=0){
    Calculatorx1();
    }
    else
    { window.location="impossible.html";}
    }


    function Calculatorx1(){
    var a=document.calculator.a.value;
    var b=document.calculator.b.value;
    var c=document.calculator.c.value;

    var aq1=b*b;
    var aw1=4*a*c;
    var delta1=aq-aw;
    var deltaroot=sqrt(delta1);
    var doublea= 2*a;
    var bminus=-1*b;
    var topx1=bminus-deltaroot;
    var x1=topx1/doublea;
    var topx2=bminus+deltaroot;
    var x2=topx2/doublea;
    }
    document.write("x2"<br>"x1");
    // End -->
    </SCRIPT>
    </head>
    <body>
    <div id="main">
    <form name=calculator>
    Résoudre une equation<br>
    <small> Entrer votre equation dans les cases en respectant l'emplacement.
    </small>
    <br>
    <input type=box name=a>x²
    <input type=box name=b>x
    <input type=box name=c>
    <input type=button value="calculer" onClick="Calculator()">
    </form>
    </div>
    </body>
    </html>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    <script type="text/javascript">
    
    function Calculator(){ 
    var a=parseInt(document.getElementById('a').value);
    var b=parseInt(document.getElementById('b').value);
    var c=parseInt(document.getElementById('c').value);
    var aq=b*b;
    var aw=4*a*c;
    alert(aq)
    var delta=aq-aw;
    
    if(delta>=0)
     {
     Calculatorx1();
     }
     else
     { 
    	window.location="impossible.html";}
     }
    }
    // the rest of code
    </script>
    Code:
    <input type="text" name="a" id="a">x&#178;
    <input type="text" name="b" id="b">x
    <input type="text" name="c" id="c">
    <input type="button" value="calculer" onclick="Calculator()">
    Hints
    1) There is no type=box
    2)Always enclose the attribute value in double quotes.
    3) Use lower-case for all tags.
    4) To do arithmetic operations on values, first parse them to int.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Jan 2008
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    But now the script donsen't work at all where as it did work before......
    Is "text" the right type of attribute for the box?

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,908
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Difficult to understand what this script is supposed to do, but two errors stand out:-

    var delta1=aq1-aw1;
    var deltaroot= Math.sqrt(delta1);

    I entered 2, 3 and 4 in the boxes, which resulted in delta being -23.

    document.write(x2 + "<br>" + x1);
    Presumably this line should be within the function.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,851
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by timothymarvel View Post
    But now the script donsen't work at all where as it did work before......
    Is "text" the right type of attribute for the box?
    Post your modified code. Also use the [code] () to wrap your code.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Jan 2008
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    My script is supposed calculate a --ax"squared"+bx+c-- type of equation.

    The script I have now is but when I try to use it on firefox it dosent work. The script I have on the top of the page worked half way because it would send you to a page when "delta" was smaller than 0(zero) but wouldn't continue id "delta" was bigger...
    Code:
    <html>
    <head>
    <title>Calculator</title>
    <script type="text/javascript">
    <!-- Begin
    function Calculator(){
    var a=parseInt(document.getElementById('a').value);
    var b=parseInt(document.getElementById('b').value);
    var c=parseInt(document.getElementById('c').value);
    var aq=b*b;
    var aw=4*a*c;
    alert(aq)
    var delta=aq-aw;
    
    if(delta>=0)
     {
     Calculatorx1();
     }
     else
     { 
    	window.location="impossible.html";}
     }
    }
    
    function Calculatorx1(){
    var a=parseInt(document.getElementById('a').value);
    var b=parseInt(document.getElementById('b').value);
    var c=parseInt(document.getElementById('c').value);
    var aq1=b*b;
    var aw1=4*a*c;
    var delta1=aq1-aw1;
    var deltaroot=Math.sqrt(delta1);
    var doublea= 2*a;
    var bminus=-1*b;
    var topx1=bminus-deltaroot;
    var x1=topx1/doublea;
    var topx2=bminus+deltaroot;
    var x2=topx2/doublea;
    }
    document.write(x2"<br>"x1);
    // End -->
    </SCRIPT>
    </head>
    <body>
    <div id="main">
    <form name=calculator>
    Résoudre une equation<br>
    <small> Entrer votre equation dans les cases en respectant l'emplacement.
    </small>
    <br>
    <input type="text" name="a" id="a">x²
    <input type="text" name="b" id="b">x
    <input type="text" name="c" id="c">
    <input type="button" value="calculer" onClick="Calculator()">
    </form>
    </div>
    </body>
    </html>


  •  

    Posting Permissions

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