string concatenation using + in Javascript

Asked

Viewed 211 times

0

var nome= "João";
var sobrenome="Silva";
var idade=30;
var resultado= nome +"" + sobrenome + 'terá' +  idade  +""+ 'anos';
console.log(resultado);

The exercise statement asks: declare a variable with a result name and assign a string joining its variables as follows: "João Silva will be 30 years old" and use the console.log to show what happens. Respect the spaces!

I created the code but it is giving error, what is wrong?

  • The code is right, only the spaces are missing. Instead of concatenating the strings with "", put the " " (With a space between quotes). Spaces count as characters.

  • Hi! A suggestion, you could do so to add spaces: var resultado = [nome, sobrenome, 'terá', idade', 'anos'].join(' ');

1 answer

2


Your code is correct, you just forgot the spaces.

var nome = "João";
var sobrenome ="Silva";
var idade = 30;
var resultado = nome + " " + sobrenome + ' terá ' +  idade  + ' anos';
console.log(resultado);

Now you’ll get your result right!

  • obirgada, there is some online code space broker?

  • I’ve never seen anything like it... It may be easier to work with template strings, because it becomes much more explicit how your string will be, I think it’s great, if you don’t know, it’s very worth taking a look: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings

  • It does not exist for this problem. In fact, the use of spaces varies a lot. In practice it is not very common to have problems with this. You forgot to check the spaces in the texts you created for display on the console. I don’t think it will happen again. :)

  • 2

    @Anapaula has no sense a code corrector for spaces, pq concatenation can be anything that will form a string, this ai was not like a spelling error, it was simply lack of spaces.

Browser other questions tagged

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