What is my error in this code?

1
Hello, I'm starting to study the JS language and I packed it here, I do not know what my mistake, could someone explain to me what I'm doing wrong?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>PAGINA</title>
  </head>
  <body>


  <script type="text/javascript">
    function calculaRetangulo(b,h) {
      var area = (b*h);
      var perimetro = (b+h)*2;
      return[area,perimetro];
    };
  </script>

<button type="button" onclick=" var resposta = calculaRetangulo(5,10); alert("AREA: " + resultado[0]); alert("Perimetro" + resultado[1])">EXECUTAR</button>


  </body>
</html>
    
asked by anonymous 05.08.2017 / 03:47

2 answers

1

Never use double quotes ( " ) inside double quotation marks, or single quotation marks ( ' ) within single quotation marks, or if you need to do so, you'll have to use a technique to escape the string using something from the type \string para escapar\ , otherwise you will be closing the attribute, if you open the browser console you can see how it reads the code snippet:

Wheninfactitwassupposedtohavebeenreadlikethis:

Youalsoconfusedtheresponsevariablewiththeresultvariablethatdidnotevenexist.

Thecorrectcodecouldbelikethis

function calculaRetangulo(b, h) {
  var area = (b * h);
  var perimetro = (b + h) * 2;
  return [area, perimetro];
};
<button type="button" onclick="var resposta = calculaRetangulo(5,10); alert('AREA: '+resposta[0]); alert('Perimetro: '+resposta[1])">EXECUTAR</button>
    
05.08.2017 / 03:53
2

You are using double quotation marks to start onClick() and within% itself onClick() you are also using double quotation marks which is what is causing this error, when you make onClick="... and put another double quotation mark right away What is interpreted is that onClick() has already ended for example:

<button onClick="alert("oi")">click</button>
                      /\ - o onClick termina aqui

The correct method would be:

<button onClick="alert('oi')">click</button>
                      /\ aspas simples

Always take this as a rule, if you start with double quotation marks use single quotation marks inside them and come and go.

    
05.08.2017 / 04:09