The problem consists of solving a quadratic equation, for D> 0, A> 0.

But it is necessary:

First print the smaller and then the larger of the found roots. The roots of the quadratic equation are found by the formula ...

Is it possible to do it at all without using the conditions ?? JS language.

Start code:

var a = +prompt("Введите значение коэффициента А (A > 0)", ""); var b = +prompt("Введите значение коэффициента B", ""); var c = +prompt("Введите значение коэффициента C", ""); var discriminant = ( (b * b) - (4 * a * c) ); var x1 = ( -b + Math.sqrt(discriminant) ) / ( 2 * a ); var x2 = ( -b - Math.sqrt(discriminant) ) / ( 2 * a ); 

Closed due to the fact that off-topic participants Dmitry Polyanin , user207618, Air , kizoso , Edward 1 Feb '18 at 20:29 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Dmitry Polyanin, Community Spirit, kizoso, Edward
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Math.max ([x [, y [, ...]]]) - Alexander
  • Ok, I haven’t seen this method, but I’ll get the most, and how can I get the least? I do not see how I register in the code, choose the remaining X. Or is it still possible, if you think? upd. Yes, Math.min. Thank! - tranntus
  • one
    Well, in the most extreme case, min(a,b)=(a+b+abs(ab))/2 max(a,b)=a+b-min(a,b) - Akina
  • one
    If A is the senior coefficient, and D is the discriminant, and it is known that A > 0 and D > 0 , then this is a task for the kindergarten. What is the difficulty then? - AnT

2 answers 2

Here are two roots:

 x = (-b ± sqrt(d)) / (2*a) 

Obviously, for a> 0, the root c - less than the root c + .

 x_min = (-b - sqrt(d)) / (2*a) x_max = (-b + sqrt(d)) / (2*a) 

    You can use Math.max / Math.min :

     Math.max(x, y) Math.min(x, y) 

    Snippet:

     // уравнение ax^2 + bx + c = 0 // например, x^2 - 3x + 2 = 0 let a = +1; let b = -3; let c = +2; let d = Math.sqrt(b * b - 4 * a * c); let root1 = (-b + d) / (2 * a); let root2 = (-b - d) / (2 * a); let rootMin = Math.min(root1, root2); let rootMax = Math.max(root1, root2); console.log(rootMin, rootMax); 

    Thank you, next time I will search carefully before asking a question.

    • If the condition is possible, use the ternary operator - Alexander
    • The section in which the task is located implies that I only know how variables are declared and values ​​are written to them. + elementary operators. - tranntus pm
    • 3
      But is it impossible without Min and Max to determine which of the roots is less, try to think logically. - Dmitry Polyanin
    • Read very carefully the expression code of the first root and the second. - Dmitry Polyanin
    • 2
      @tranntus and in vain, one of the fundamentals of programming is logic, and there’s no point in abandoning it. for example, there are numbers a1 = 1 and a2 = 2, and you need to output them in ascending order, that this should be compared through sort? In addition, the Min and Max if functions are included inside. - Dmitry Polyanin