I've been doing Asabeneh 30 day JavaScript roadmap and in day 7, level 2, there is an exercise that asks us to build a quadratic function solver. I've tried my function with a couple of numbers and seems to work out just fine, but when plugging in some other numbers (for instance, periodic numbers as roots) the function just outputs two random values
Example:
function solveQuadEquation (a = 0, b = 0, c = 0) {
let x1 = ((-1 * b + Math.sqrt(b * b - 4 * a * c)) / 2 * a)
let x2 = ((-1 * b - Math.sqrt(b * b - 4 * a * c)) / 2 * a)
let discriminant = Math.pow(b, 2) - 4 * a * c
if (discriminant > 0) {
return 'The function has two solutions: ' + x1 + ' , ' + x2
} else if (discriminant === 0) {
return 'The function has two repeated roots: ' + x1
} else {
return 'the function has no real values'
}
return [x1, x2];
}
console.log(solveQuadEquation(9, 3, -4))
The console outputs
// The function has two real values: 42.161192594... , -69.161192594...
any ideas on what might be wrong with it?
thanks!!