What was my mistake with that code?

Asked

Viewed 77 times

1

Hello, I’m starting to study the JS language and I stopped here, I don’t know what my mistake, someone could 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>
  • 1

    Instead of using double quotes on alert(), use single quotes. You are already using double quotes around the code, so when you included in the alert the quotes open and close several times

2 answers

1


Never use double quotes (") in double quotes or single quotes (') inside simple quotes, or if it is necessary to do this, you will have to use a technique to escape the string using something like \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:

inserir a descrição da imagem aqui

When it was meant to be read like this:

inserir a descrição da imagem aqui

In addition, you confused the response variable with the result variable that did not exist.

The correct code could be like this

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>

  • 1

    Contunia same thing, console points it -> Uncaught Syntaxerror: Unexpected token }

  • I’ll check here and I’ll give you the answer

  • 1

    Leone, include the explanation of what your code does and what difference it makes to what’s in the question. So it’s easy for anyone who finds that answer later.

  • 1

    I did some research on what parseint is

  • The correct code is this, I was wrong about parseint()

  • You, besides the quotation marks, confused answer with result

  • 1

    Uhum, more now I understand what my mistakes were, vlw by help

  • Please return my help by marking my answer as the correct one by clicking on the green v-shaped icon next to the answer

  • 1

    Although the code solves the problem, the answer in no way explains what was wrong in the AP code, so I gave -1, if explain the error I withdraw the vote -1 =)

  • was in the comments I will move on to the reply obg by the clarification @Marceloboni

  • I added my two cents to your reply, can revert to the previous state if you feel the need

Show 6 more comments

1

You are using double quotes to start the onClick() and within the onClick() you are also using double quotes which is what is generating this error when you do onClick="... and put another double quote right after what is interpreted is that the onClick() is already finished 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 turn and reverse.

  • 1

    I get it... VLW for the help

  • 1

    If the answer is correct please mark as solved (the "v" sign on the left side)

  • 1

    @Leocaracciolo done.

  • 1

    now yes, much better, +1

Browser other questions tagged

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