English

# how to solve two equations with programming - Javascript

solving equations

## Snippet Code

Following are the steps to solve any two linear equations using javascript:1. Change the linear equations into array notation example: eq1 => 5x 4y = 33 eq2 => 6x 2y = 13 will be,//first equation var eq1 = new Array(3); eq1[0]=5; eq1[1]=4; eq1[2]=33;//second equation var eq2 = new Array(3); eq2[0]=6; eq2[1]=2; eq2[2]=13; 2. Add the 'solveEqns' and 'findDeter' functions in your code and call the above 'solveEqns' function and store the result in an array. var ans = new Array(2); ans=solveEqns(eq1,eq2);3. Show the x value and y value document.write("x= " ans[0] "
y= " ans[1]);

<script> //include this script in the head tag //function to solve two equations function solveEqns(eq1,eq2) { var ans = new Array(2); var a1=eq1[0]; var b1=eq1[1]; var c1=eq1[2]; var a2=eq2[0]; var b2=eq2[1]; var c2=eq2[2]; ans[0]=findDeter(c1,b1,c2,b2)/findDeter(a1,b1,a2,b2); ans[1]=findDeter(a1,c1,a2,c2)/findDeter(a1,b1,a2,b2); //alert(ans); return ans; } //function to find the determinant function findDeter(a,b,c,d) { var d=(a*d)-(c*b); return d; } </script>