How to play a var inside Return?

Asked

Viewed 173 times

2

function tamanhoNomeCompleto(a,b){
  var tNC = a.length + " ".length + b.length;
  return tNC;
}

tamanhoNomeCompleto("Paulo","Paulada");

How do I put the tNC in the return?

'Cause I’m getting this:

tamanhoNomeCompleto uses unnecessary local variables you can return the expression directly

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

4

That?

function tamanhoNomeCompleto(a, b){
    return a.length + b.length + 1;
}

console.log(tamanhoNomeCompleto("Paulo", "Paulada"));

I put in the Github for future reference.

To variable is totally unnecessary there so just carry the expression to the return.

Variables are meant to store values, if you don’t need to save, don’t create one, use the value you’re storing in it directly where you need it.

Note that I didn’t get the size of the space because it is known (I don’t know if all browsers or Node would optimize this.

  • Thanks a lot friend, I was going crazy to figure out how to do this in Return, because I’m learning and I get really pissed when I can’t find the answer to my problem. And you taught how I use console.log for a function I couldn’t see!

  • 1

    @Marcelrobertopiesigilli see the [tour] on the best way to say thank you.

Browser other questions tagged

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