Display message on screen from an object

Asked

Viewed 145 times

-2

I have the following object:

obj = {nome: 'Elis', sobrenome: 'Regina', profissao: 'cantora'}

I need to write the message on the screen:

"Elis Regina was a great singer".

Can someone help me?

  • 2

    You know how to use the operator .? For example, objeto.campo? Knows the function alert?

  • document.write(obj.nome + ' ' + obj.sobrenome + ' foi uma grande ' + obj.profissao)

2 answers

1

You can create a function that consumes this type of objects and return the complete String. The ideal would be to have the gender in the object so you could dynamically change the uma for um.

Example:

const descrever = obj => `${obj.nome} ${obj.sobrenome} foi ${obj.genero === 'F' ? 'uma' : 'um'} grande ${obj.profissao}`;

const Elis = {
  nome: 'Elis',
  sobrenome: 'Regina',
  profissao: 'cantora',
  genero: 'F'
};
const Vivaldi = {
  nome: 'Antonio',
  sobrenome: 'Vivaldi',
  profissao: 'compositor',
  genero: 'M'
};

console.log(descrever(Elis)); // Elis Regina foi uma grande cantora
console.log(descrever(Vivaldi)); // Antonio Vivaldi foi um grande compositor

1

Robert, to get the expected result, you need to concatenate the object items and display in a alert, for example.

Note that to be able to extract the object name, we need to indicate which object name, and then which attribute we want, for example, to recover only the name, we can do so:

obj = {nome: 'Elis', sobrenome: 'Regina', profissao: 'cantora'}

console.log(obj.nome);

To know what console is.log, click here


So calling each attribute and concatenating, we have the result:

obj = {nome: 'Elis', sobrenome: 'Regina', profissao: 'cantora'}

//Elis Regina foi uma grande cantora.
alert(obj.nome +' '+ obj.sobrenome + ' foi uma grande '+obj.profissao + '.');

To know what concatenation is, click here

  • Write a Javascript feature that asks the user to enter your name and then write your name on the console in a welcome message. For example, if the user’s name is Alex, the message should be: "Hello Alex, welcome!". Resolution tip: use Javascript’s prompt() function.

  • 2

    @Robertoliveira Which part of the answer you didn’t understand?

  • 1

    Mark this answer as solved in the "Correct" symbol above. That you wrote is another question. And please ask with your words and not as a list execises. Post your questions.

Browser other questions tagged

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