Advertisement
AleksandarKostadinov

19. Quadratic equation

Jan 19th, 2018
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveQuadraticEquation(a, b, c) {
  2.     let d = getDiscriminant(a, b, c);
  3.  
  4.     if (b === 0 && c === 0) {
  5.         console.log(0);
  6.         return;
  7.     }
  8.  
  9.     if (d < 0) {
  10.         console.log("No");
  11.         return;
  12.     }
  13.  
  14.     let roots = getRoots(a, b, d);
  15.  
  16.     for (let root of roots.filter((v, i, f) => f.indexOf(v) === i).sort()) {
  17.         console.log(root);
  18.     }
  19.  
  20.     function getRoots(aa, bb, dd) {
  21.         let result = [];
  22.        
  23.         result.push((-bb - Math.sqrt(dd)) / (2 * aa));
  24.         result.push((-bb + Math.sqrt(dd)) / (2 * aa));
  25.  
  26.         return result;
  27.     }
  28.  
  29.     function getDiscriminant(a, b, c) {
  30.         return (b ** 2 - 4 * a * c);
  31.     }
  32. }
  33.  
  34. //solveQuadraticEquation(6, 11, - 35);
  35. //solveQuadraticEquation(1, - 12, 36);
  36. //solveQuadraticEquation(5, 2, 1);
  37. solveQuadraticEquation(-4, 3, 4);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement