My functions only return 0

Asked

Viewed 148 times

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:&emsp;<input id="a" type="number">
      <br>
      <br>
      Digite o B:&emsp;<input id="b" type="number">
      <br>
      <br>
      Digite o C:&emsp;<input id="c" type="number">
      <br>
      <input type="submit">
   </form>
</div>

4 answers

6


In addition to what they have already said in the other answers (you should only take the values after the Submit and the string values must be converted to numbers), it can simplify the code, making the function already returns all the roots in an array. This prevents you - unnecessarily - to calculate the delta twice.

Another important point is to check if the delta is negative, and in this case the equation will have no real roots.

// retorna um array com as raízes reais
function bhaskara(a, b, c) {
    var delta = (b * b) - (4 * a * c);
    if (delta < 0) {
        // não tem raízes reais, retornar array vazio
        return [];
    }

    if (delta == 0) { // só tem uma raiz
        return [ -b / 2 * a ];
    }

    var raizDelta = Math.sqrt(delta);
    // retorna as duas raízes em um array
    return [ (-b + raizDelta) / 2 * a, (-b - raizDelta) / 2 * a ];
}

form.addEventListener('submit', function (e) {
    e.preventDefault();
    // só pega os valores após o submit
    var a = parseFloat(document.getElementById("a").value);
    var b = parseFloat(document.getElementById("b").value);
    var c = parseFloat(document.getElementById("c").value);
    var raizes = bhaskara(a, b, c);
    if (raizes.length > 0) { // tem pelo menos uma raiz
        alert(raizes.join(' '));
    } else { // não tem nenhuma raiz real
        alert("Não tem raízes reais");
    }
})
<div>
   <form id="form">
      Digite o A:&emsp;<input id="a" type="number">
      <br>
      <br>
      Digite o B:&emsp;<input id="b" type="number">
      <br>
      <br>
      Digite o C:&emsp;<input id="c" type="number">
      <br>
      <input type="submit">
   </form>
</div>

Since the function can return an array with one or two roots, I used the method join to print the roots separated by space (if you have only one, print only this value, and if you have two, print both separated by a space).

4

I haven’t looked at if the formula is wrong, but the problem isn’t because of the formula. You are ordering to take the "input" data and do all the calculations before the input data, then the data is 0 and then the calculation is 0. You have to ask to do all this, after the submit is triggered, so you can’t just call the alert() in the event, it has to be all the code that picks up the data and calls the calculation.

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 = (-bb + Math.sqrt(delta0)) / 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;
}
form.addEventListener('submit', function () {
  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);
    alert(bh1 + " " + bh2);
})
<div>
   <form id="form">
      Digite o A:&emsp;<input id="a" type="number">
      <br>
      <br>
      Digite o B:&emsp;<input id="b" type="number">
      <br>
      <br>
      Digite o C:&emsp;<input id="c" type="number">
      <br>
      <input type="submit">
   </form>
</div>

I put in the Github for future reference.

3

Place the variables within the scope of Submit, so the values will be assigned in Submit, and the Math.sqrt method does not accept negative values, then return Nan (Not a number).

var form = document.getElementById("form");
form.addEventListener('submit', function () {

  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);

    alert(bh1 + " " + bh2);
});

function bhaskaraP(ab, bb, cb) {
  var delta = Math.pow(bb, 2) - 4 * ab * bb;
  delta = (delta < 0) ? delta * -1 : delta;
  return - bb + Math.sqrt(delta, 2)/2*ab;
}

function bhaskaraN(ab, bb, cb) {
    var delta = Math.pow(bb, 2) - 4 * ab * bb;
  delta = (delta < 0) ? delta * -1 : delta;
  return - bb - Math.sqrt(delta, 2)/2*ab;
}
  • 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)

  • 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.

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

  • 1

    OK ,thanks for the help

3

Some mistakes:

  1. You have not parsed the values received from HTML, after all, all HTML values are strings

  2. The calculation should only be run in Ubmit, after the user has entered the values.

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
}
form.addEventListener('submit', function (e) {
	e.preventDefault()
	var form = document.getElementById("form")
	var a = document.getElementById("a")
	var b = document.getElementById("b")
	var c = document.getElementById("c")
	var ax = parseFloat(a.value)
	var bx = parseFloat(b.value)
	var cx = parseFloat(c.value)
	var bh1 = bhaskaraP(ax, bx, cx)
	var bh2 = bhaskaraN(ax, bx, cx)
	alert(bh1 + " " + bh2)
})
<div>
   <form id="form">
      Digite o A:&emsp;<input id="a" type="number">
      <br>
      <br>
      Digite o B:&emsp;<input id="b" type="number">
      <br>
      <br>
      Digite o C:&emsp;<input id="c" type="number">
      <br>
      <input type="submit">
   </form>
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.