TL; DR
It is unlikely that you need a method that gives the size of a full name without creating this full name, so this method makes no sense (I’m speculating, I don’t know details of the problem), the solution would be to see what is real need and do this, would probably create the full name and this new object has exactly the size you want in a natural and simple way.
If you want to insist on the error of having this function the correct thing would be to do this:
function tamanhoNomeCompleto(nome, sobrenome) {
return nome.length + sobrenome.length + 1;
}
console.log(tamanhoNomeCompleto('Lila', 'Oliveira'))
Other answers
The current answers work, but are not "right", for a basic reason, they allocate memory without need just to give a purely mathematical information. This may not be a problem in most applications, it may even be noticeable, but why do something quite costly that puts pressure on Garbage Collector can generate unexpected breaks for zero gain? Right is to do the best that can, not only give the right result.
The idea of Leandrade looks good (although in detail give a wrong result) because you will probably have to have the full name, so it would make more sense to return a new object created with the full name, and then the allocation is not unnecessary. It turns out the problem of the question doesn’t talk about it, so it’s a speculation, a good one, but it’s still not the problem presented, but I don’t even want to cling to it.
The problem is that a function called tamanhoNomeCompleto()
results in something that is not the size of the full name, also does this, but not only, it is a array or an object that has it and something else. This form violates the principle of sole responsibility, not that this principle should not be infringed, but should at least change the name of the method.
But if you do this, the method loses much meaning, and then perhaps the question has already conceived wrong. If you need the full name at some point, and I think you do (I may be mistaken), why don’t you just create it? Then finding the size of it becomes simpler than creating a function for it and the question problem does not exist.
This function should be replaced by one that generates the full name, and then just take the length
(Note that the question is spelled wrong, everyone misses, I do straight) that name. The function may or may not be interesting because the operation becomes canonical, if need be, shorter to call, and avoids any possible error by exposing the direct concatenation mechanism.
If you have the idea of Leandrade (in general I do not think a good solution, it could be useful if I understand the exact problem, with common sense I could adopt in some case) it could be this:
function nomeCompletoETamanho(nome, sobrenome) {
return {
tamanho: nome.length + sobrenome.length + 1,
nomeCompleto: nome + ' ' + sobrenome
}
}
var nomeETamanho = nomeCompletoETamanho('Lila', 'Oliveira');
console.log(nomeETamanho.nomeCompleto);
console.log(nomeETamanho.tamanho);
I put in the Github for future reference.
A user in a chat indicated that he could create a variable with the full name and return it in addition to returning its size, increase a line but avoid the arithmetic used there because it is precisely what I said above, creates the object and picks up its size.
In his code does not take the size, just print the result of the function, which is an object or array, If you take the content, compare my code with his:
nome.length
nomeCompletoETamanho.tamanho
To be readable I had to give a significant name to the variable and was even redundant, because it is difficult to give name to an object that has things that has not cohesion. Cohesion is so bad that the second member has the same information present in the first member, the size is already in the nomeCompleto
.
If you do with array then legibility goes in the bag so it can be shorter, mine and his:
nome.length
nome[0]
What a nome[0]
you mean? You only know if you see the implementation of the function, it is a abstraction leak.
I love these questions because they give the chance to show in practice some concepts that I have already answered :)
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero