The code below is not running. Only when I remove the variables Width and Height from within the functions

Asked

Viewed 73 times

-1

Is it possible for someone to answer this question? It should be simple, but it’s breaking my head. The code below is not running. Only when I remove the variables Width and Height from within the functions

function mostrarAreaAlert() {
  var largura = prompt("Digite a largura");
  largura = parseFloat(largura);

  var altura = prompt("Digite a altura");
  altura = parseFloat(altura);

  var area = calcularArea(largura, altura);

  alert('A area total é: ' + area);
}

function mostrarAreaOutput() {
  var largura = prompt("Digite a largura");
  largura = parseFloat(largura);

  var altura = prompt("Digite a altura");
  altura = parseFloat(altura);

  var area = calcularArea(largura, altura);

  document.querySelector('#output').innerHTML = 'A area total é: ' + area;
}

function calcularArea(l, a) {
  return l * a;
}
<body>


  <button onclick="mostrarAreaAlert(largura, altura)">Mostrar Área em Alert</button>

  <button onclick="mostrarAreaOutput(largura, altura)">Mostrar Área no Output</button>

  <div id="output"></div>



</body>

2 answers

1


Hello. From what I understand, it is not necessary to pass the two variables within the functions because soon they will be initialized when the questions are answered. I gave a modified in your code to make it more reusable. I hope I helped.

<button onclick="mostrarAreaAlert()">Mostrar Área em Alert</button>
<button onclick="mostrarAreaOutput()">Mostrar Área no Output</button>

<div id="output"></div>

<script>
    function question() {
        var res = [];

        let largura = prompt("Digite a largura");
        res[0] = parseFloat(largura);

        let altura = prompt("Digite a altura");
        res[1] = parseFloat(altura);

        return res;
    }

    function mostrarAreaAlert() {
        let res = question();

        let area = calcularArea(res[0], res[1]);
        alert('A area total é: ' + area);
    }

    function mostrarAreaOutput() {
        let res = question();

        let area = calcularArea(res[0], res[1]);
        document.querySelector('#output').innerHTML = 'A area total é: ' + area;
    }

    function calcularArea(l, a) {
        return l * a;
    }
</script>
  • Perfect Fernando, this answer was very useful too. Our friend @Asleep gave a comment above, where he answered precisely the question I had, why my code didn’t run. He reported that it was only removing width and height from inside the onclickmouse of the Uttons, and really solved. Anyway, thank you so much for the time you devoted to answering my question, because it was as useful as the other, I learned another way to accomplish with your comment. Thank you very much.

-1

solution 1

var area = largura * altura

Then you put the value to be presented where you want.


solution 2

What you did in this next function would work if it was width and height, you don’t have anything started with l and a.

function calcularArea(l, a){
        return l * a;
    }

You know what I mean?

  • Thank you very much for your willingness to attend to me, but I still don’t understand. Could you tailor my code as explained above? Based on this, I would understand what I have to do to get the prompt to just click on the Uttons. Get it? Thank you so much already.

  • 1

    @Lucasrodrigues, if you did not understand the answer because you marked it as accepted by closing your question? How and why to accept an answer?

  • To solve your problem, according to your code, just change the following in it: In Buttons, when you call by displaareaalert( ), do not need the width and height inside, leave the ( ) empty in both and tell me if it worked just like this.

  • @Augustovasques Sorry, I’m still learning to stir here. It was my fault, thanks for the guidance.

  • @Asleep, perfect. It worked according to your suggestion. Thank you very much my friend!!!!!

Browser other questions tagged

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