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.