Concatenate string and return the number of characters

Asked

Viewed 104 times

-1

This is what I need to do:

1 - Create a function called tamanhoNomeCompleto
2 - I received name and surname as parameter, ie two parameters 3 - This function will return the full size, counting an extra space to separate both
4 - I’m using the length (length) to count letters and spaces
5 - I am passing as parameter to string 'Juan Perez' <= note that there is a space between the names
6 - Precise execution of the function tamanhoNomeCompleto me return 10 <= number of characters and space in 'Juan Perez'

I did the following way, but my result is Nan. I wonder what is wrong?

function tamanhoNomeCompleto(nome,sobrenome) {
    return ('Juan'.lenght+' '.lenght+'Perez'.lenght);
}
tamanhoNomeCompleto('Juan','Perez');

2 answers

1


There are some mistakes there.

The most obvious is that you have made a typo in the name of the property that gives the size of the text, the correct is length. Javascript instead of giving error and quit, tries to do the account anyway, so gives a crazy result.

But actually the function doesn’t even make sense, it’s for you to do the size of the data according to the received parameters and not with literals. Also you don’t need to take the size of 1 space because the result will always be 1 so you can add this directly, it’s not that it doesn’t work, but it doesn’t make sense, get used to making a code simpler and, fast, and even readable.

function tamanhoNomeCompleto(nome,sobrenome) {
    return nome.length + sobrenome.length + 1;
}
console.log(tamanhoNomeCompleto('Juan', 'Perez'));

I put in the Github for future reference.

  • Resolved issue. I understood your logic. Thank you Mineiro !

0

There are some errors in its function:

  • You used the property length with the incorrect writing, was written lenght, fixing that your function will already work.

  • You received the parameters but did not use them in the function, you fixed the names inside the function, so it only worked for the name Juan Perez.


See below your function with the appropriate corrections:

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

const tamanhoJuan = tamanhoNomeCompleto('Juan', 'Perez');

console.log( tamanhoJuan );

const tamanhoMarcelo = tamanhoNomeCompleto('Marcelo', 'Maciel');

console.log( tamanhoMarcelo );

The estate length has been corrected, with this will no longer be returning NaN in the sum of the sizes, in addition now the parameters nome and sobrenome are being used in the function, making it dynamic, being able to work with several names.

Obs.: How the size of a space will always be 1, you could remove the length of space and add +1 in the end, it will also work.


Documentations:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Fun%C3%A7%C3%B5es

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions

Browser other questions tagged

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