0

So i just started JS a while back, and im halfway through the course in Code academy. Sadly my exams came in between so i forgot a lot of what i learnt, so i decided to create a program, a very simple one to like refresh my memory. Now i googled how to run a js doc, and it gave me this: FYI, ive tried to use the project builder on CA and im currently using WebStorm as my IDE. (in an html file)

<!DOCTYPE html>

<head>
    <script language ="javascript" src ="code.js"/>
    <script language ="javascript">
            main();
        }
    </script>
    <title>Equation Solver</title>
</head>
<body>

</body>
</html>

My function's name is main, and i basically tell it im using js and call the function. both the html file and code file are in the same folder. The program im working on is a quadratic/linear equation solver, ive only done the quadratic part using the quadratic formula [{-b±√(b²-4*a*c)}/2] where is equation is of the form ax²+bx+c=0.

SO heres the code, it doesnt even run. im including this because maybe i miswrote teh code or something.:

var main =function(){//Linear in 2 start
    var choice = prompt("Choose your type of equation : Type 1 for linear in 2 variables, 2 for quadratic in one variable ");
    if(choice =1){
        alert("we are currently working on this feature, please select 2, or wait for an update :)");
    }//linear in 2 end
    else if(choice = 2){//quadratic start
        alert("the equation is of the form : ax^2 + bx + c = 0 , only input the coefficients i.e - the value of ax^2 is a, or the value of bx is b, not bx. The value of b for the equation 5x^2 + 7x +3 is 7, not 7x");
        var a =  prompt("Put in the value of a");//declaring variables
        var b =  prompt("Put in the value of b, if the bx part of the equation doesn't exist, input 0. Ex for equation 2x^+6=0 , b =0, since its technically 2x^2 + 0b + 6 = 0");
        var c =  prompt("Put in the value of c, if the c part of the equation doesn't exist, input 0. Ex for equation 2x^+6x=0 , c =0, since its technically 2x^2 + 6b + 0 = 0");
        var D = ((b*b)-(4*a*c));//computing discriminant
        if(D < 0){
            alert("The quadratic equation doesn't have real roots; the closest value is : " + (-b/2) +"i/2");
        }
        else{
            root1 = (-b+D)/(2*a);
            root2 = (-b-D)/(2*a);
        }
        if(D=0){
            console.log("Both roots are equal, their value is " + root1);
            alert("Both roots are equal, their value is " + root1);
        }
        else if ( D > 0){
            console.log("The roots of the equation are: " + root1 + root2);
            alert("The roots of the equation are: " + root1 + root2 );
        }

    }//quadratic end

}

main();

var again = confirm("wanna solve another equation?");

if(again = true){
    main();
}

not sure what I'm doing wrong. Any help is appreciated :D. Thanks guys.

l2aelba
  • 21,591
  • 22
  • 102
  • 138

2 Answers2

2

<script> tag is not self closed tag, it has opening or closing tag

Wrong way

<script language ="javascript" src ="code.js"/>

You should add javascript file like this

<script language ="javascript" src ="code.js">  </script>
//---------------------------------add closing tag---^

As Smeegs pointed out

= not comparsion but assignment operator.

should be

if(again == true){

instead of

if(again = true){

Suman Bogati
  • 6,289
  • 1
  • 23
  • 34
0

There are some errors in your code.

Don't use the language attribute of script tags, use type="text/javascript" instead. The first <script> tag is missing the closing tag. The second one has a random { after the call to main();

This is how you should do it:

<script type="text/javascript" src ="code.js"></script>
<script type="text/javascript">
        main();
</script>

In every conditional you've used the wrong operator. In JavaScript (and many other languages) the = operator is used for assignment, not for comparison. So if you write if (x = 1) {...} you're actually setting x to the value 1. Use the == operator to test for equality.

if(choice == 1){
...
if(D == 0){
...
if (again == true) {
....
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user3417400
  • 341
  • 2
  • 4
  • i just looked it up and it says i have to use === if comparing with 0 or a boolean value. That right? – t3hCrush3r Mar 27 '14 at 14:39
  • Yes, there is a subtil diference betwen == and ===, i recommend you use === every time, because it checks for strict equality, it will only return true if two things are exactly the same thing. To give you an example, 0 == false returns true, 0 === false returns false. – user3417400 Mar 27 '14 at 15:06
  • So basically, == is for values, === is for object AND value? – t3hCrush3r Mar 27 '14 at 15:16
  • Basically, === is for everything and == is broken and sometimes it'll make your codes fail silently. That's at least how i think. – user3417400 Mar 27 '14 at 15:53