How do I create the variable that saves all this information and then displays it on the.log() console?

Asked

Viewed 190 times

0

Create a function that given the following object:

var endereco = {
 rua: "Rua dos pinheiros",
 numero: 1293,
 bairro: "Centro",
 cidade: "São Paulo",
 uf: "SP"
};

Return the following content:

The user lives in São Paulo / SP, in the Centro neighborhood, on the street "Rua dos Pinheiros" with nº 1293.

I did it like this, but it’s not right:

    <script>
        var endereco = {
            rua: 'dos Pinheiros',
            num: 1293,
            bairro: 'Centro',
            cidade: 'São Paulo',
            uf: 'SP'
        };

        function paraString(endereco) {
            return 'O usuário mora em' ${endereco.cidade} '/' ${endereco.uf}', no bairro' ${endereco.bairo}', na rua' ${endereco.rua} 'com nº' ${endereco.num}.
        };

        console.log(paraString(endereco));

    </script>
  • Is why you used single quotes when it should use crase ``

  • Valew! It was that msm

2 answers

2

I put it in a variable to see if it’s getting the addresses correctly
And there was one more mistake where you were using double quotes

<script>
        var endereco = {
            rua: 'dos Pinheiros',
            num: 1293,
            bairro: 'Centro',
            cidade: 'São Paulo',
            uf: 'SP'
        };

        function paraString(endereco) {
            var string = "O usuário mora em" + endereco.cidade +  "/ "+ endereco.uf + ", no bairro " + endereco.bairro + ", na rua" + endereco.rua + " com nº " + endereco.num;
            return string;
        };

        console.log(paraString(endereco));

    </script>
  • Valew, that’s it msm! Thank you very much man.

2


Hello,

In Javascript, when you’re going to use ${variable}, you’re talking about template strings, which begins and ends with ` (crase).

I made a modification to your code (and I packed a few things, like changing bairo for bairro in the method), the correct way is this:

var endereco = {
   rua: "dos Pinheiros",
   num: 1293,
   bairro: "Centro",
   cidade: "São Paulo",
   uf: "SP"
};

function paraString(endereco) {
   return `O usuário mora em ${endereco.cidade}/${endereco.uf}, no bairro ${endereco.bairro}, na rua ${endereco.rua} com nº ${endereco.num}.`;
}

console.log(paraString(endereco));

Browser other questions tagged

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