My functions only return 0

5

I was trying to make the Bhaskara formula into functions, but something went wrong and only returns 0 no matter what inputs I put in. Here is 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>
    
asked by anonymous 19.11.2018 / 23:40

4 answers

5

In addition to what you have already said in the other answers (you should only get the values after the submit and the string values should be converted to numbers), simplify the code, function already returns all the roots in an array. This prevents you - unnecessarily - computing 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 () {
    // 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 #

    
20.11.2018 / 00:17
4

I did not look at whether the formula is wrong, but the problem is not because of the formula. You are prompting to get the data "entered" and do all the calculations before the data is entered, then the data is 0 and the calculation gives the same. You have to ask to do all this, after submit is triggered, then you can not just call alert() in the event, it has to be all the code that takes 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>
    
20.11.2018 / 00:04
3

Put the variables within the scope of the submit, so the values will be assigned in the submit, and the Math.sqrt method does not accept negative values, thus returning 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;
}
    
20.11.2018 / 00:03
3

Some errors:

  • You have not parsed values received from HTML, after all, all HTML values are strings

  • The calculation should only be performed on the submit, 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>
        
    20.11.2018 / 00:06