Doubt about JS function

Asked

Viewed 80 times

0

I have the myFunction function that calculates the product of two numbers and displays them on the screen. I would like to shift this function to the Listen function when the value of P1=10. The way it is, it only displays the value of myFunction, and ignores the equality P1=10 to make the detour. Can help me?

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p id="demo"></p>

<script>
function myFunction(p1, p2) {

if (p1=10)  {listen(a)}
    return p1 * p2;
}
document.getElementById("demo").innerHTML = myFunction(10, 3);


var a ="jj";

function listen(k){
 var s1 = k;
 alert(s1);
 return k
 }

</script>

</body>
</html>
  • I gave an answer to the main problem of your code, but I’m intrigued by the function listen. What’s your role? Just make one alert? why the return? will be integrated into the result of myFunction?

2 answers

1

In your code, besides doing an assignment here:

if (p1=10) // deveria ser if (p1==10)

You are defining a after calling the function:

document.getElementById("demo").innerHTML = myFunction(10, 3); 
var a ="jj";

That’s why a is undefined - Move var a="jj"; for before function:

var a ="jj";
document.getElementById("demo").innerHTML = myFunction(10, 3);

And I believe the program will already work as you want. I changed your code a little bit, just to make it easier to test, click on Execute just below the code:

function myFunction(p1, p2) {
    if (p1==10)  {listen(a)}
    else alert(p1 * p2);
}

function listen(k){
    var s1 = k;
    alert(s1);
    return k
}

var a ="jj";
<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p id="demo"></p>
<input type="number" id="valor1" value=10>
<input type="number" id="valor2" value=3>
<button onclick="myFunction(document.getElementById('valor1').value,
  document.getElementById('valor2').value)">Testar Função</button>

</body>
</html>

  • now it worked , it was just what needed, grateful

  • @Silezia please mark one of the answers as accepted by clicking on the mark to the left of the text! :)

0

When you only use = in Javascript this is attribution and not comparison.

That is, what p1=10 does is that p1 becomes real 10.

You must use == to compare the value, or === to compare the value and of the same type.

Suggestion:

var a = "jj";

function myFunction(p1, p2) {
  if (p1 == 10) return listen(a);
  else return p1 * p2;
}

function listen(k) {
  return k
}

document.getElementById("demo").innerHTML = myFunction(10, 3);
<h2>JavaScript Functions</h2>

<p id="demo"></p>

  • the Listen function, can be any one, for example "value",

  • the function Listen, can be any one, for example "value", What I want is to deviate to the second function, when there is comparison. I did what Voce said, put == and also === but gives the value Undefined and the multiplication P1*P2. With numbers , instead of writing "a" in the function, it works. Because it doesn’t work with letters, I wanted the answer when it was comparison, was "JJ" understood

  • @Silezia is still not very clear to me what you are looking for. Take a look at the suggestion I have just put in the answer. That is what you are looking for?

  • @Silezia is still not very clear to me what you are looking for. Take a look at the suggestion I have just put in the answer. That is what you are looking for?

Browser other questions tagged

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