Use of length to count concatenated strings

Asked

Viewed 776 times

-2

I need to solve this exercise but it is appearing with error.

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

in the case, the parameters have to appear for example tamanhoNomeCompleto("Enzo", "Silva") returns 10, but only the name is appearing and is not listed.

  • 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).

3 answers

3

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 :)

  • I confess that although the question does not say clearly that it is to return the name + surname, I assumed this by the return of it that has quotes separating, as a sign that wanted to not only return the size of the strings added, but as tbm the full name.

  • @Leandrade found the general idea "good", so much so that I used it, but then I would need to do it the right way. As I said I think the general idea is good, but the way to do it is not, and would be applied in some specific scenario. One thing I didn’t mention is that in the background when it returns the full name the size is attached implicitly, note that I used nomeETamanho.tamanho to get the size. If I only got the full name I would do just that: nome.length, much shorter, right? In your answer you do not even take the data alone, have the object printed, which is never what the person wants.

  • If you do with the array gets shorter, but loses the semantics, would do something like this nome[0] to get the size. Look at that complicated thing to read, what does that mean? I prefer to write nome.length, a little longer, but very readable. Agrees?

  • Yes of course, I agree, it’s kind of complicated to give an exact answer when the question is not 100% clear. I only left one way to do for her code, sure if it was another scenario, with all the necessary information, would have implemented otherwise.

  • @Leandrade I thought until she is clear in a certain sense, but it is not considered that it is probably an XY problem. I doubt that you need a function that generates the size of the full name without generating the full name, and if you generate it, that function has no sense of existence anymore. I edited the answer to give more details of this.

  • All right man, I agree.

Show 1 more comment

1

You won’t get it that way because it needs to concatenate strings first. for you to use the length.

that way your code will work

function tamanhoNomeCompleto(nome, sobrenome) {
    return (nome + ' ' + sobrenome).length
}
console.log(tamanhoNomeCompleto('Kusther', 'Developer'));
  • 1

    Remembering that this way, you will not put the white space between the name is the last name, unless you pass the last name with white space first in the parameters. But that’s the thing.

  • Ready I have modified.

1

Aside from what Artsher Developer exemplified in his answer, the Return mode he did returns only the first element found, that is, the name. For you to print also the full name for example, can return a objeto { } or a array [ ]:

Object:

function tamanhoNomeCompleto(nome, sobrenome) {
  return {
    tamanho: nome.length + sobrenome.length, 
    nome_completo: nome +' '+ sobrenome
  }
}

console.log(tamanhoNomeCompleto('Enzo', 'Silva'))

Array:

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

console.log(tamanhoNomeCompleto('Enzo', 'Silva'))

Browser other questions tagged

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