6
I was trying to do Bhaskara’s formula in functions, but something went wrong and only return 0
no matter what inputs I put in. Here’s the code:
function bhaskaraP(ab, bb, cb) {
var Mb = bb * -1
var b2 = bb * bb
var delta0 = b2 - 4 * ab * cb
var delta = Math.sqrt(delta0)
var rstM = (Mb + delta) / 2 * ab
return rstM
}
function bhaskaraN(ac, bc, cc) {
var Mbc = bc * -1
var b2c = bc * bc
var delta0c = b2c - 4 * ac * cc
var deltac = Math.sqrt(delta0c)
var rstN = (Mbc - deltac) / 2 * ac
return rstN
}
var form = document.getElementById("form")
var a = document.getElementById("a")
var b = document.getElementById("b")
var c = document.getElementById("c")
var ax = a.value
var bx = b.value
var cx = c.value
var bh1 = bhaskaraP(ax, bx, cx)
var bh2 = bhaskaraN(ax, bx, cx)
form.addEventListener('submit', function () {
alert(bh1 + " " + bh2)
})
<div>
<form id="form">
Digite o A: <input id="a" type="number">
<br>
<br>
Digite o B: <input id="b" type="number">
<br>
<br>
Digite o C: <input id="c" type="number">
<br>
<input type="submit">
</form>
</div>
good to know that Math.sqrt does not accept negative value, but what this"?" in the delta serves? (if this is obvious ,sorry I don’t know much about javascript and tals)
– 0000FFcar
is an "if" on a line, i.e., if the delta value is less than 0 then multiply by -1 if it does not play the delta.
– Wictor Chaves
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
– Wictor Chaves
OK ,thanks for the help
– 0000FFcar