Beginner in Javascipt

Asked

Viewed 2,492 times

1

Guys I need a help, I’m doing some exercises and I stuck in 3 specific on string, it’s simple but I’ve tried everything and I can’t find the mistakes.

Ex 1: Create a function perimeter that tells us the perimeter of a circle when we give it the radius as parameter. Also the function area which gives us the area of a circle when it receives the radius as parameter.

    function  perimetro(raio) {
    var perimetro = π * raio * 2;
    return perimetro;
}
    function area(raio) {
    var area = π * raio * raio;
    return area;
} 
\\Retorna dizendo que as variáveis locais são desnecessárias e que poderia retornar a expressão diretamente.

Ex 2: Create a function called sizeNomeComplete, which takes a name and a surname as parameters, and which will return the full size, counting an extra space to separate both.

    function tamanhoNomeCompleto(nome, sobrenome) {
    var NomeCompleto = (nome.lenght + sobrenome.length);
    var tamanhoNomeCompleto = 6;
    return tamanhoNomeCompleto; 
 }

Ex 3: Create a function write Letter, which takes as parameters a title, a name and a surname and returns a single string as a result.

    function escreverCartao() {
    var titulo = "Dra";
    var nome = "Ana";
    var sobrenome = "Pérez";
    var escreverCartao = (titulo + " " + nome + " " + sobrenome);
    return titulo + " " + nome + " " + sobrenome

6 answers

4

In Ex 1, it is not an error but only a warning, because you are creating a stop variable to return a value that could be returned directly, e.g..:

function  perimetro(raio) {
   return π * raio * 2;
}

In Ex 2, vc is setting a variable with value 6 and returned, that is, any value that passes by parameter the return will always be 6.

function tamanhoNomeCompleto(nome, sobrenome) {
    return nome.length + sobrenome.length + 1; // + 1 seria para contar o espaço
}

In Ex 3, vc is not passing variables by Paramento, but creating locally with a static value.

function escreverCartao(titulo, nome, sobrenome) {
    return titulo + " " + nome + " " + sobrenome
}
  • 2

    And as for the variable π in the first?

  • Thanks for the help with the exercises, in 1 and 3 worked correctly your solution but in 2 still presents error "Nan == 10" I used a random number

  • I believe you might be using nome.lenght instead of nome.length, as you used in the question example

1

In Ex 3. It worked for me this way:

function escreverCartao(titulo=“Dra.”, nome=“Ana”, sobrenome=“Pérez”) {
    return titulo + “ “ + nome + “ “ + sobrenome;
}

Replace the information with the content you want!! :)

1


In EX2: Following @Rick’s suggestion, but considering that for learning is always good a better clarity and break less called the function length, I suggest the following code:

function tamanhoNomeCompleto(nome, sobrenome) {
    return (nome + " " + sobrenome).length;
}

0

In the first example do so:

function  perimetro(raio) {
   return 3.14 * raio * 2;
}
    function area(raio) {
    return 3.14 * raio * raio;
} 

It worked for me.

-1

In Example count name and on name worked like this

function tamanhoNomeCompleto(Juan, Perez) {
    return Juan.length + Perez.length + 1; // + 1 seria para contar o espaço
}

-3

The following is a response from EX 2:

function tamanhoNomeCompleto(nome, sobrenome) {
    return nome.length + " ".length + sobrenome.length
}

Browser other questions tagged

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