Implementation of the algorithm solving a quadratic equation in JavaScript


square equation

<!DOCTYPE html>

function linear(a,b) {
  if (a==0)
    if (b==0)
      alert("Identity equation");
      alert("Contrary equation");
  else {
    var x = -b / a;
    alert("x = " + x);

var a=prompt("a:", "");
var b=prompt("b:", "");
var c=prompt("c:", "");
if (a == 0) 
else {
  var delta = b*b-4*a*c;
  if (delta < 0)
    alert("Lack of solutions");
    if (delta == 0) {
      var x = -b / (2 * a);
      alert("One solution x = " + x);
    else {
      var x1 = (-b + Math.sqrt(delta)) / (2 * a);
      var x2 = (-b - Math.sqrt(delta)) / (2 * a);
      alert("x1 = " + x1 + "    x2 = " + x2);


Program line Description Algorithm block
 1 In the 1st line of the program, we inform the browser that our document was written in HTML (more precisely, it is a header specific to HTML 5). Line 2 contains the <html> tag that begins the HTML code. The 3rd line starts the <body> section of our document, containing the elements that are to appear on the page. In our example, the <body> section contains only a script written in JavaScript that implements the algorithm for solving a quadratic equation. The <script> tag on line 4 informs about the start of the script. -
6 - 16 We define the function linear that implements an algorithm for solving a linear equation. 4
18-20 We declare the variables a,b,c using the var keyword. At the same time, the assignment operator ("=") initializes these variables with user-entered values using the prompt() statement. prompt - JavaScript standard function - displays a text input pop-up on the screen. The first argument of the function specifies the text displayed as a prompt, the second is the default value entered in the edit field. As a result, the prompt() function returns the text entered by the user into the edit field and then confirmed with the OK button. 2
21 We check if the coefficient a is equal to 0. If so, we are dealing with a linear equation. 3
22 We will be in this line only when a = 0, i.e. when the equation is a linear equation. We call the function linear(b, c), which performs the algorithm for solving a linear equation. It is worth noting that the coefficient to the first power of x is denoted by b, and the intercept is c (traditionally, a and b are used respectively in a linear equation). 4
23 else, i.e. in the opposite case. At this point, the solution of the equation begins when a ≠ 0, i.e. when we are actually dealing with a quadratic equation. -
24 We declare a delta variable and calculate its value. 5
25 We check if delta < 0. 6
26 When delta < 0, we use the alert() function to print an appropriate message informing about the lack of solutions. 7
27 This is where the program block starts when delta is not lower than zero, i.e. when the equation has solutions (one or two). -
28 We check if delta = 0. 8
29 We calculate and save in the variable x the solution of the equation (one double - delta = 0). 9
30 We display the calculated result. 10
32 We start the program block executed when the delta = 0 condition was not met (and the earlier delta < 0 and a ≠ 0). -
33 - 34 We calculate the solutions and assign their values to x1 and x2 variables, which are also declared using the var instruction. 11
35 We display the calculated result - two solutions of the quadratic equation. 12
46-37 We close blocks of compound statements. -
39-41 We close the tags </script>, </body> i </html>. -