Javascript - function Math.log() does not load

Asked

Viewed 36 times

-1

Good afternoon,

I have the following problem. I am creating a button to calular logarithm inside a calculator. so I found in the link on this link the possibility to make Lulo. I appreciate the help. The result was to be displayed on the display but everything is empty.

function logaritmo( x, y) {
return Math.log(y) / Math.log(x);
} 

But I want the above function to be executed in the algorithm below.

function calculoLogaritmo() {
var vis = document.calcform.visor;
var element = document.getElementById('elemento');
var logaritmo = document.getElementById('log');
var base = document.getElementById('base');
var resultado;
element.innerHTML = 'O logaritmo de <input id="log" type="text"/> na base   <input id="base" type="text"/> ';
if(isNaN(Number(logaritmo.value)) && isNaN(Number(base.value)) || Number(logaritmo.value) == "" && Number(base.value) == "" ){
alert("Favor informa valores validos para a execução do resultado");
} else{
resultado = logaritmo(Number(base.value), Number(logaritmo.value));
}
vis.value = resultado;

} 

Follows the html

<form name="calcform" method="post" action="">
<p id="elemento"></p>
     <input type="text" name="visor" id="visor" value=""/>
<td><input type="button" name="logaritmo" class="formula" value="log" title="logaritmo" onclick="calculoLogaritmo()" /></td>
</form>
  • And what behavior was expected? What behavior is occurring?

  • The expected behavior was that the result appears in the display but it is empty.

  • Put that information in your question then

  • The name logaritmo is used 2x in your code, both for the function you are using to do the calculation and for the HTML element you are picking up with document.getElementById(). This could be your problem, rename one of the two.

1 answer

1


You had placed a variable with the same name of the function and also had not created the elements yet. Fixing we will have:

function calcular(x, y) {
  return Math.log(y) / Math.log(x);
}

function calculoLogaritmo() {
  var vis = document.getElementById('visor');
  var element = document.getElementById('elemento');
  var logaritmo = document.getElementById('log').value;
  var base = document.getElementById('base').value;
  var resultado;
  element.innerHTML = `O logaritmo de ${logaritmo} na base   ${base} `;
  if (isNaN(Number(logaritmo)) && isNaN(Number(base)) || Number(logaritmo) == "" && Number(base) == "") {
    alert("Favor informa valores validos para a execução do resultado");
  } else {
    resultado = calcular(Number(base), Number(logaritmo));
  }
  vis.value = resultado;

}
<form name="calcform" method="post" action="">
  <p id="elemento"></p>
  <input id="log" type="text"/>
  <input id="base" type="text"/>
  <input type="text" name="visor" id="visor" value="" />
  <input type="button" name="logaritmo" class="formula" value="log" title="logaritmo" onclick="calculoLogaritmo()" />
</form>

  • A doubt! What does ${logarithm} do?

  • It inserts the value of the variable into the string template, resulting in the string with the value inside

Browser other questions tagged

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